srlabs / centaur
Cartalyst's Sentinel for Laravel 5 的有见地的实现
Requires
- php: ^8.2
- cartalyst/sentinel: ^8.0
- illuminate/support: ^11.0
Requires (Dev)
- mockery/mockery: ^1.4.4
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^10.5
Suggests
- vinkla/hashids: A Hashids bridge for Laravel
README
本软件包为 Laravel 5 提供了对 Cartalyst Sentinel 的有见地的实现。
如果您正在使用较旧的 Laravel 版本,还有其他可用的 版本。
安装
通过 Composer 安装软件包
$ composer require srlabs/centaur
将服务提供者添加到您的 config/app.php
文件中
'providers' => array( ... Centaur\CentaurServiceProvider::class, ... )
此软件包不会使用 自动包发现 - 您需要手动注册它。这是故意的。
在新应用程序中的使用
如果您正在创建一个新的 Laravel 5.* 应用程序,此软件包提供了一个快速启动 Cartalyst\Sentinel
的便捷方式。首先,删除新 Laravel 5.1 应用程序中提供的默认认证框架
$ php artisan centaur:spruce
接下来,使用 Centaur 的框架命令在您的应用程序中创建基本的认证控制器和视图
$ php artisan centaur:scaffold
发布 Cartalyst\Sentinel
资产
$ php artisan vendor:publish --provider="Cartalyst\Sentinel\Laravel\SentinelServiceProvider"
运行数据库迁移
$ php artisan migrate
运行数据库播种器。在运行此操作之前,您可能需要重新生成自动加载器
$ composer dump-autoload
$ php artisan db:seed --class="SentinelDatabaseSeeder"
您还需要将这些路由添加到您的 routes.php
文件中
// Authorization Route::get('login', 'Auth\SessionController@getLogin')->name('auth.login.form'); Route::post('login', 'Auth\SessionController@postLogin')->name('auth.login.attempt'); Route::any('logout', 'Auth\SessionController@getLogout')->name('auth.logout'); // Registration Route::get('register', 'Auth\RegistrationController@getRegister')->name('auth.register.form'); Route::post('register', 'Auth\RegistrationController@postRegister')->name('auth.register.attempt'); // Activation Route::get('activate/{code}', 'Auth\RegistrationController@getActivate')->name('auth.activation.attempt'); Route::get('resend', 'Auth\RegistrationController@getResend')->name('auth.activation.request'); Route::post('resend', 'Auth\RegistrationController@postResend')->name('auth.activation.resend'); // Password Reset Route::get('password/reset/{code}', 'Auth\PasswordController@getReset')->name('auth.password.reset.form'); Route::post('password/reset/{code}', 'Auth\PasswordController@postReset')->name('auth.password.reset.attempt'); Route::get('password/reset', 'Auth\PasswordController@getRequest')->name('auth.password.request.form'); Route::post('password/reset', 'Auth\PasswordController@postRequest')->name('auth.password.request.attempt'); // Users Route::resource('users', 'UserController'); // Roles Route::resource('roles', 'RoleController'); // Dashboard Route::get('dashboard', function () { return view('Centaur::dashboard'); })->name('dashboard');
这仅作为一个起点;您可以根据需要更改它们。在做出任何更改之前,请确保您已经阅读了您的新认证控制器并理解了它们是如何工作的。
Centaur 自动安装 Sentinel 并为您注册了 Sentinel
、Activations
和 Reminders
别名。有关使用 Sentinel 的详细说明,请参阅 此处。
如果您决定使用 Laravel 的 Route::resource()
选项,您需要使用 表单方法欺骗 来访问一些生成的路由。
在现有应用程序中的使用
如果您已经构建了认证视图和控制器,最佳做法是将 AuthManager
注入到您的控制器中,并将其用作 Sentinel 的包装器。有关 AuthManager
方法和响应的详细信息,请参阅 此处。
使用自定义中间件
可能存在这种情况,即此软件包附带的中件件的某些行为可能不符合您的确切需求。要调整中件件,请在您的 app/Http/Middleware
目录中复制有问题的 Centaur 中间件类 - 这个新类可以取任何您喜欢的名字。然后,您可以调整控制器和/或路由文件中的中件件引用以使用新类,或者您可以将新类绑定到 App 服务提供者中的 Centaur 中间件类名,如下所示
// app/providers/AppServiceProvider.php /** * Register any application services. * * @return void */ public function register() { $this->app->bind('Centaur\Middleware\SentinelGuest', function ($app) { return new \App\Http\Middleware\AlternativeGuestMiddleware; }); }