安裝一個(gè)全新的laravel框架
composer create-project laravel/laravel permission --prefer-dist
cd permission
composer require spatie/laravel-permission
如果出現(xiàn)
$ composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
You made a reference to a non-existent script @php artisan package:discover
這個(gè)報(bào)錯(cuò)說明composer版本不夠 我用的git打開的命令行所以使用composer.phar
升級(jí)composer
升級(jí)命令
$ composer.phar selfupdate
再執(zhí)行
$ composer.phar install
想要在Laravel中使用Entrust,首先需要通過Composer來安裝其依賴包:
composer require zizaco/entrust 5.2.x-dev
安裝完成后需要在config/app.php中注冊(cè)服務(wù)提供者到providers數(shù)組:
Zizaco\Entrust\EntrustServiceProvider::class,
同時(shí)在該配置文件中注冊(cè)相應(yīng)門面到aliases數(shù)組:
'Entrust' => Zizaco\Entrust\EntrustFacade::class,
如果你想要使用中間件(要求Laravel 5.1或更高版本)還需要添加如下代碼到app/Http/Kernel.php的routeMiddleware數(shù)組:
'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
2树埠、配置
在配置文件config/auth.php中設(shè)置合適的值矩屁,Entrust會(huì)使用這些配置值來選擇相應(yīng)的用戶表和模型類:
'providers' => [
? ? 'users' => [
? ? ? ? 'driver' => 'eloquent',
? ? ? ? 'model' => App\User::class,
? ? ? ? 'table' => 'users',
? ? ],
],
你還可以發(fā)布該擴(kuò)展包的配置以便后續(xù)自定義相關(guān)表名以及模型類的命名空間:
安裝自動(dòng)加載
$ composer.phar dump-autoload
php artisan vendor:publish
選擇包含entrust 的數(shù)字編碼?
該命令會(huì)在config目錄下創(chuàng)建一個(gè)entrust.php文件怕吴。
接下來我們使用Entrust提供的遷移命令生成遷移文件:
php artisan entrust:migration
我這步報(bào)錯(cuò)了 報(bào)了這個(gè)錯(cuò)
$ php artisan entrust:migration
? ReflectionException? : Method Zizaco\Entrust\MigrationCommand::handle() does
not exist
是因?yàn)樵贛igrationCommand.php這個(gè)文件下面找不到handle()這個(gè)方法喻奥,把文件中的fire方法改成handle()這個(gè)方法汉买。
再執(zhí)行
$ php artisan entrust:migration
會(huì)生成一個(gè)遷移文件
生成數(shù)據(jù)表
$ php artisan migrate
報(bào)錯(cuò)滋将!
$ php artisan migrate
? Illuminate\Database\QueryException? : SQLSTATE[42000]: Syntax error or access
violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: a
lter table `roles` add unique `roles_name_unique`(`name`))
問題根源
MySql支持的utf8編碼最大字符長(zhǎng)度為3字節(jié)邻悬,如果遇到4字節(jié)的寬字符就會(huì)出現(xiàn)插入異常。三個(gè)字節(jié)UTF-8最大能編碼的Unicode字符是0xffff随闽,即Unicode中的基本多文種平面(BMP)父丰。因而包括Emoji表情(Emoji是一種特殊的Unicode編碼)在內(nèi)的非基本多文種平面的Unicode字符都無法使用MySql的utf8字符集存儲(chǔ)。
解決問題
升級(jí)MySql版本到5.5.3以上掘宪。
手動(dòng)配置遷移命令migrate生成的默認(rèn)字符串長(zhǎng)度蛾扇,在AppServiceProvider中調(diào)用Schema::defaultStringLength方法來實(shí)現(xiàn)配置:
public function boot()
{
//
? ? Schema::defaultStringLength(191);
}
再執(zhí)行
$ php artisan migrate
會(huì)生成四張表
CREATE TABLE `permission_role` (
? `permission_id` int(10) unsigned NOT NULL,
? `role_id` int(10) unsigned NOT NULL,
? PRIMARY KEY (`permission_id`,`role_id`),
? KEY `permission_role_role_id_foreign` (`role_id`),
? CONSTRAINT `permission_role_permission_id_foreign` FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
? CONSTRAINT `permission_role_role_id_foreign` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `permissions` (
? `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
? `name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
? `display_name` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
? `description` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
? `created_at` timestamp NULL DEFAULT NULL,
? `updated_at` timestamp NULL DEFAULT NULL,
? PRIMARY KEY (`id`),
? UNIQUE KEY `permissions_name_unique` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `role_user` (
? `user_id` int(10) unsigned NOT NULL,
? `role_id` int(10) unsigned NOT NULL,
? PRIMARY KEY (`user_id`,`role_id`),
? KEY `role_user_role_id_foreign` (`role_id`),
? CONSTRAINT `role_user_role_id_foreign` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
? CONSTRAINT `role_user_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `roles` (
? `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
? `name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
? `display_name` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
? `description` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
? `created_at` timestamp NULL DEFAULT NULL,
? `updated_at` timestamp NULL DEFAULT NULL,
? PRIMARY KEY (`id`),
? UNIQUE KEY `roles_name_unique` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `users` (
? `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
? `name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
? `email` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
? `password` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
? `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
? `created_at` timestamp NULL DEFAULT NULL,
? `updated_at` timestamp NULL DEFAULT NULL,
? PRIMARY KEY (`id`),
? UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;