This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
准备开发
开发环境
推荐本地安装 composer 、 php 7.3+、 pnpm
本地必须安装 node
需要学会使用 Laravel 8、 Vue 3
相关文档
Nginx
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}
后台
pnpm i
pnpm dev
pnpm build
npm install -g pnpm
Mix后台
部署流程
通过模板创建仓库
创建本地测试环境
本地clone项目
mysql.server start
创建数据库 utf8mb4_general_ci
创建.env文件,修改内容
composer install
php artisan key:generate
php artisan storage:link
php artisan migrate
php artisan serve
部署服务器
服务器clone项目
部署宝塔,创建数据库
创建.env文件,修改内容
composer install
chown www.www storage/ -R
php artisan storage:link
php artisan migrate
部署后台
本地修改后台域名
pnpm build
git push
配置Git
[credential]
helper = store
常用命令行
php artisan make:model User -mc 创建 控制器 + 数据库
生成数据库
php artisan migrate 追加
php artisan migrate:fresh 初始化
php artisan make:controller YoController 创建控制器
常用代码段
接口
Yo
return
Yo :: echo ( $data = []);
Yo :: create_echo ( $id );
Yo :: update_echo ( $id );
Yo :: delete_echo ( $id );
// 输出
{
"code" : 200 ,
"message" : "增加成功" ,
"data" : {
"id" : 12
}
}
exit
Yo :: exit ( $data ); // 会以 JSON 的形式输出 $data 的内容。
Yo :: error_echo ( $code , $replace = []);
// code 在 /config/code.php 中配置
// $replace 为 ? 替换字符
// Yo::error_echo(100001, ['替换内容']);
// 在 code.php 中 100001 => '?固定内容',
// 输出
{
"code" : 100001 ,
"message" : "替换内容固定内容"
}
// Yo::error_echo(100002);
// 输出
{
"code" : 100002 ,
"message" : "错误信息"
}
Lu
Lu::ssl() 返回是不是 HTTPS
Lu::ip() 返回用户 IP
Lu::ge($str) 去掉字符串空格
Lu::fp($path) 返回当前域名完整路径
Lu::date($time = false, $format = "Y-m-d H:i:s") 返回格式化时间
Lu::time() 返回毫秒时间戳
Lu::post($url, $data, $type = 'json') 发送 POST 请求
Login
Login::admin($auth_id = 0) 检查权限,在数据库 db.auths 中配置,没有权限会直接退出
Login::admin_check($auth_id = 0) 检查权限,没有权限会 return
增删改查
<? php
namespace App\Http\Controllers ;
use App\Http\Requests\EditUserInput ;
use App\Models\User ;
use Login ;
use Yo ;
class WordBankController extends Controller
{
public function create ( EditUserInput $request )
{
Login :: admin ([ 1 ]);
$name = $request -> post ( 'name' );
$order = $request -> post ( 'order' );
$user = new User ();
$user -> name = $name ;
$user -> order = $order ;
$user -> save ();
return Yo :: create_echo ( $user -> id );
}
public function delete ()
{
Login :: admin ([ 1 ]);
$id = request () -> post ( 'id' );
User :: where ( 'id' , $id ) -> delete ();
return Yo :: delete_echo ( $id );
}
public function update ( EditWordBankInput $request )
{
Login :: admin ([ 1 ]);
$id = request () -> post ( 'id' );
$name = $request -> post ( 'name' );
$order = $request -> post ( 'order' );
$user = User :: find ( $id );
if ( ! $user ) Yo :: error_echo ( 100021 , [ '用户' ]);
$user -> name = $name ;
$user -> order = $order ;
$user -> save ();
return Yo :: update_echo ( $user -> id );
}
public function list ()
{
$user_list = User :: orderBy ( 'order' , 'desc' ) -> get ();
return Yo :: echo ([ 'list' => $list ]);
}
}
Requests
<? php
namespace App\Http\Requests ;
use Illuminate\Contracts\Validation\Validator ;
use Illuminate\Foundation\Http\FormRequest ;
use Yo ;
class EditUserInput extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize ()
{
return true ;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules ()
{
return [
'name' => [ 'required' , 'between:1,30' ],
];
}
public function messages ()
{
return [
'name.required' => 100001 ,
'name.between' => 100002 ,
];
}
public function failedValidation ( Validator $validator )
{
Yo :: error_echo ( $validator -> errors () -> first ());
}
}
后台
新建页面 b 时需要创建 a.vue 和 a/b.vue 两个文件
a.vue
< script setup ></ script >
< template >
< router-view > < /router-view>
</ template >
< route >
{ redirect : '/a/b' , "meta" : { "title" : "多级菜单" }}
</ route >
a/b.vue
< script setup >
/**
* name:
* user: sa0ChunLuyu
* date: 2022年10月8日 16:56:44
*/
</ script >
< template >
< div >
< n-card title = "多级菜单" >
< div ></ div >
< /n-card>
</ div >
</ template >
< style scoped >
</ style >
< route >
{ "meta" : { "title" : "多级菜单" }}
</ route >