Laravel? ???? ??? ?? ?? ??? ???? ??
? ??????? ????? ?? ?????? ??? ?? ??? ?? ? ????? ????. ?? ???? PHP ?????? Laravel? ??? ?? ??? ???? ?? ?? ??? ??? ??? ?????. ? ???? Laravel? ???? ??? ?? ?? ??? ???? ??? ???? ???? ?? ??? ?????.
- ?????? ???
?? ???, ?? ? ?? ?? ??? ??? ?????? ??? ????? ???. ??? ????? ?? Laravel? ?? ?????? ??? ???? ?????? ???? ???????. ??? ??? ?? ???? ?? ????? ??? ? ?? ??? ???? ?????? ??? ????.
php artisan make:migration create_roles_table --create=roles php artisan make:migration create_permissions_table --create=permissions php artisan make:migration create_role_user_table --create=role_user php artisan make:migration create_permission_role_table --create=permission_role
?? ?? database/migrations
?????? ??? ?????? ??? ?? ?????. ??? ?? ?????. database/migrations
目錄中找到生成的遷移文件,并編輯它們。以下為示例代碼:
// roles表遷移文件 public function up() { Schema::create('roles', function (Blueprint $table) { $table->increments('id'); $table->string('name')->unique(); $table->timestamps(); }); } // permissions表遷移文件 public function up() { Schema::create('permissions', function (Blueprint $table) { $table->increments('id'); $table->string('name')->unique(); $table->timestamps(); }); } // role_user關(guān)聯(lián)表遷移文件 public function up() { Schema::create('role_user', function (Blueprint $table) { $table->integer('role_id')->unsigned(); $table->integer('user_id')->unsigned(); $table->foreign('role_id')->references('id')->on('roles'); $table->foreign('user_id')->references('id')->on('users'); }); } // permission_role關(guān)聯(lián)表遷移文件 public function up() { Schema::create('permission_role', function (Blueprint $table) { $table->integer('permission_id')->unsigned(); $table->integer('role_id')->unsigned(); $table->foreign('permission_id')->references('id')->on('permissions'); $table->foreign('role_id')->references('id')->on('roles'); }); }
完成遷移文件的編輯后,運行以下命令來執(zhí)行遷移:
php artisan migrate
- 創(chuàng)建模型和關(guān)系
接下來,我們需要創(chuàng)建Laravel模型來映射數(shù)據(jù)庫表并建立它們之間的關(guān)系。打開命令行工具,輸入以下命令生成模型文件:
php artisan make:model Role php artisan make:model Permission
然后打開生成的模型文件,并添加以下代碼:
// Role模型 class Role extends Model { public function users() { return $this->belongsToMany(User::class); } public function permissions() { return $this->belongsToMany(Permission::class); } } // Permission模型 class Permission extends Model { public function roles() { return $this->belongsToMany(Role::class); } }
- 添加用戶關(guān)聯(lián)
打開User
模型文件,在類中添加以下方法:
public function roles() { return $this->belongsToMany(Role::class); } public function hasRole($role) { if (is_string($role)) { return $this->roles->contains('name', $role); } return !! $role->intersect($this->roles)->count(); } public function assignRole($role) { return $this->roles()->save( Role::whereName($role)->firstOrFail() ); }
代碼中,Role
模型使用了belongsToMany
方法建立了與User
模型之間的多對多關(guān)系,hasRole
方法用于判斷用戶是否擁有某個角色,assignRole
方法用于給用戶分配角色。
- 添加權(quán)限關(guān)聯(lián)
在Role
模型中,我們已經(jīng)定義了與Permission
模型之間的多對多關(guān)系,因此可以直接使用已有的方法。
- 中間件配置
Laravel提供了中間件功能來實現(xiàn)對路由的權(quán)限控制,我們需要配置中間件來限制用戶的訪問。打開app/Http/Kernel.php
文件,在$routeMiddleware
數(shù)組中添加以下代碼:
'role' => AppHttpMiddlewareRoleMiddleware::class, 'permission' => AppHttpMiddlewarePermissionMiddleware::class,
- 創(chuàng)建中間件
在命令行工具中,輸入以下命令生成中間件文件:
php artisan make:middleware RoleMiddleware php artisan make:middleware PermissionMiddleware
然后打開生成的中間件文件,并添加以下代碼:
// RoleMiddleware class RoleMiddleware { public function handle($request, Closure $next, $role) { if (! $request->user()->hasRole($role)) { abort(403, 'Unauthorized'); } return $next($request); } } // PermissionMiddleware class PermissionMiddleware { public function handle($request, Closure $next, $permission) { if (! $request->user()->hasPermissionTo($permission)) { abort(403, 'Unauthorized'); } return $next($request); } }
代碼中,RoleMiddleware
檢查用戶是否擁有指定角色,PermissionMiddleware
檢查用戶是否具有指定權(quán)限。
- 使用中間件
現(xiàn)在,可以在需要進(jìn)行權(quán)限控制的路由上使用我們定義的中間件來限制訪問。在路由文件中,使用middleware
Route::get('/admin', function () { // 限制只有擁有admin角色的用戶才能訪問 })->middleware('role:admin'); Route::get('/delete-user', function () { // 限制只有擁有delete-user權(quán)限的用戶才能訪問 })->middleware('permission:delete-user');?????? ?? ??? ??? ? ?? ??? ???? ??????? ?????.
rrreee
- ?? ? ?? ??
???? ?? ? ?? Laravel ??? ???? ?????? ???? ???? ??? ?? ??? ?????. ??? ??? ?? ?? ??? ???? ?? ??? ?????.
rrreee???? ?? ??? ?? ??? ?? ?? ??? ?????. ??rrreee- ????? ?? ????????Open User ?? ???? ???? ?? ???? ?????: ??rrreee??????
Role
??? belongsToMany
???? ???? ??? ?????. User
?? ?? ??? ????? hasRole
???? ???? ????? ?? ??? ??? ???? assRole
???? ????? ??? ???? ? ?????. ??- ???? ?? ????????
??
???? ??
??? ??? ??? ??????. ??? ?? ??? ??? ??? ? ????. ??- ?????? ????????Laravel? ??? ??? ???? ?? ???? ??? ?????. ??? ???? ????? ????? ???? ???.
app/Http/Kernel.php
??? ?? $routeMiddleware
??? ?? ??? ?????: ??rrreee- ??Create middleware???? ?? ??? ???? ?? ??? ???? ???? ??? ?????. ??rrreee???? ?? ??? ???? ??? ?? ?? ??? ?????. ??rrreee??????
RoleMiddleware
? ????? ??? ??? ??? PermissionMiddleware
? ????? ??? ??? ??? ?????. ??- ?????? ?????????? ?? ??? ?? ?? ??? ??? ??? ??? ????? ??? ? ????. ??? ???? ?? ??? ??
middleware
???? ???? ???? ??? ?????. ??rrreee?????? ??? ?? ??? ?? Laravel? ???? ??? ???? ?? ??????. ???? ?? ??? ??. ?????? ??, ??, ???? ? ?? ??? ???? ???, ?? ? ?? ?? ??? ?? ? ??? ?????. ??????: ??????? ?? ??? ? ??????? ???? ???? Laravel? ??? ??? ??? ? ?? ??? ??? ??? ?????. ? ????? Laravel? ???? ?????? ??, ?? ?? ? ???? ??? ?? ?? ??? ?? ??? ?? ?? ??? ???? ??? ???? ???? ?? ??? ?????. ? ?? ??? ?? ??? ??? ??? ????. ??? ??? Laravel? ???? ??? ?? ?? ??? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

PHP?? ?? ??? ???? ? ?? ?? ??? ????. 1. php.ini? ?? ??? ??; 2. ? ?? (? : Apache? Setenv ?? nginx? FastCGI_Param)? ??????. 3. PHP ?????? putenv () ??? ??????. ? ??? Php.ini? ????? ??? ???? ??? ???? ? ?? ??? ?? ???? ????? ???? Putenv ()? ?? ??? ?????. ?? ???? ?? ?? (? : php.ini ?? ? ?? ??)? ???? ????. ?? ?? ??? ??? ?? ??? ????? ???? ?? ????.

Laravel? ?? ??? ?? ?? ??? ?? ?? ??? ???? ??? ??????. ?? ???? ?? ??? ????? ? ???? I/O ?? ? ?? ?? ??? ???? ???? ??? ?? ? ????. 1. ?? ????? ?? ? ? ???????? ??? ????? ?? ???? ??????. 2. ??? ? ??? ?? ? ? PhPartisAnconfig? ?? ???????. 3. ?? ??? ??? ??? ???? ?? ?? ?? ???? ???? ????. 4. ?? ?? ??? ???? ?? ??? ??? .env ??? ???? ?? ???????.

PHP ????? ?? ??? ??? ? ??? ??? CI (Continuous Integration) ????? ???? ? ????. 1. DockerFile? ???? ?? ???, ?? ??, ??? ?? ? ?? ??? ???? PHP ??? ?????. 2. Gitlabci? ?? CI/CD ??? ???? .gitlab-ci.yml ??? ?? ??, ??? ? ?? ??? ???? ?? ??, ??? ? ??? ?????. 3. PHPUNIT? ?? ??? ??? ??? ???? ?? ?? ? ???? ???? ????????. 4. Kubernetes? ?? ?? ?? ??? ???? ?? .yaml ??? ?? ?? ??? ?????. 5. Dockerfile ??? ? ??? ??? ??????

??? ?? ??? PHP ???? ?? ?? ??? ???? ?? ???????. RBAC (Role-Based Access Control) ??? ?? ???, ?? ? ??? ???? ??? ?? ?? ? ??? ?????. ?? ???? ??? ?????. 1. ???, ?? ? ??? ? ???? user_roles ? role_permissions? 3 ?? ?? ???; 2. $ user-> can ( 'edit_post')? ?? ???? ?? ?? ??? ?????. 3. ??? ???? ??? ??????. 4. ?? ??? ???? ?? ?? ?? ? ??? ? ???? ???? ?? ??? ? ?? ??? ?????. 5. ??? ??? ?? ?? ???? ?? ???? "??"? ??????.

Laravel? eloquentscopes? ?? ??? ??? ??? ?????? ?? ?? ??? ????? ?????. 1. ?? ??? ???? ???? ???? ???? Post :: published (); 2. ??? ??? ?? ??? ???? ???? ?? ??? ?? ?? ?? ??? ???? ???? ??? ?????? ??? ???? ???????. 3. ????? ?? ?? ?? ??? ??? ?? ?? ??? ?? ? ? ??? ?? ? ? ?? ?? ??? ?????. 4. ?? ??? ? ??? ?? ???? ? ??? ? ?? ??, ?? ??, ?? ???? ? ?? ?????????.

CreateAhelpers.phpfileInapp/helperswithCustOmFunctionsikeFormatPrice, isactiveroute, andisAdmin.2.addTheFileTothe "??"sectionOfcomposer.jsonUnderAutoLoad.3.runcomposerDump-AUTOLOADTOMAKETHINGTICTIONSGLOBELYAVAILABLE.4.USETHEHELPERFUNCUNTION

?? ?? ?? : ?? ????? PHP? ?? Error_Log ()? ??? ? ????. ????? ???? ??? ?? ??? ?????? ???? ?? ??? ? ?? ??? ???? ??? ?? ???, ??, ?? ? ?? ? ?? ?? ??? ???? ??? ??????. 2. ??? ?? ?? : ??? ??? ??? ??? ? ??? ?? ??? ??? ?? ??? ??? ??????? ??????. MySQL/PostgreSQL? ???? ??? ? ???? ??????. Elasticsearch Kibana? ? ???/? ???? ?????. ???, ??? ?? ? ??? ? ?? ??? ?? ??????. 3. ?? ? ?? ????? : ??, ???, ?? ? ??? ??? ??????. Kibana? ?? ????? PHP ??? ?? ?? ?????? ???? ???? ?????? ???? ??? ? ?? ??? ??? ? ????.

??, ??, ?? ?? ? ?? ??? ???? ?? ??? ?? ? ?? ???? ?????. 2. ?? ???? ???? ?? ??? ??? SONGSTOMONY ? HASMANY ?? ??; 3. ?? ? ? ?? ? ?? ??? ????? (?? ???? ?? ??? ? ??). 4. ?? ? ?? ??? ???? ?? ??? ???? ?? ? ?? ??? ???? ?? ??? ?????. 5. ?? ???? ??? ?? (?? ??)? ???? ?? ????? ??????. 6. ?? ??? ?? ??? ???? Laravel Signature URL? ???? ??? ??????. 7. ? ?? ?? ? ? ?? ??? ?? ?? ??? ?? ??? ?????. ?????? ??, ?? ?? ??? ??????????.
