【问题标题】:How to use different Auth for different controller in Laravel如何在 Laravel 中为不同的控制器使用不同的 Auth
【发布时间】:2017-09-12 04:08:23
【问题描述】:

我正在 laravel 中创建一个项目。我的问题是,由于这是一个购物车,我为客户和管理员使用不同的表格。因此,如果请求是管理员,那么我想从管理员表进行身份验证,如果它来自商店,我想使用客户表进行身份验证。是否可以为控制器设置身份验证表,或者是否可以使用创建多个身份验证器而不是默认值?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    你应该使用ENTRUST (Laravel 5 Package)

    但是在你需要组织你的数据库结构之前。 对于所有类型的用户,使用您的用户表,使用外键 user_id 有一个单独的客户表。为用户分配角色,当用户登录时检查其角色并根据分配的角色重定向到他们的仪表板。

    【讨论】:

      【解决方案2】:

      Multi Auth 是一个在 Laravel 中可能面临的常见问题,因此可以创建它。

      您可以为此编写自己的代码或使用某些包来实现此特定功能。它们很容易在 github 上找到。 Example link.

      here 有一个很好的教程,我会用它来解释。

      您需要创建两个表,customers 和 admin。默认用户表可用于客户(或其他方式)。 make:auth 命令将为用户表 auth 创建所有路由、控制器和视图。

      对于管理员身份验证,首先创建一个管理员表。下一个控制器

      app/Http/Controllers/AdminAuth/AuthController
      app/Http/Controllers/AdminAuth/PasswordController
      

      编辑 config/auth.php 文件并为用户执行相同的管理员操作,在需要时使用管理员模型而不是用户。

      //Authenticating guards
      'guards' => [
          'user' =>[
              'driver' => 'session',
              'provider' => 'user',
          ],
          'admin' => [
              'driver' => 'session',
              'provider' => 'admin',
          ],
      ],  
      
      //User Providers
      'providers' => [
          'user' => [
              'driver' => 'eloquent',
              'model' => App\User::class,
          ],
          'admin' => [
              'driver' => 'eloquent',
              'model' => App\Admin::class,
          ]
      ],  
      
      //Resetting Password  
      'passwords' => [
          'clients' => [
              'provider' => 'client',
              'email' => 'auth.emails.password',
              'table' => 'password_resets',
              'expire' => 60,
          ],
          'admins' => [
              'provider' => 'admin',
              'email' => 'admin.auth.emails.password',
              'table' => 'password_resets',
              'expire' => 60,
          ],
      ],
      

      编辑路由文件

      Route::group(['middleware' => ['web']], function () {
          //Login Routes...
          Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
          Route::post('/admin/login','AdminAuth\AuthController@login');
          Route::get('/admin/logout','AdminAuth\AuthController@logout');
      
          // Registration Routes...
          Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
          Route::post('admin/register', 'AdminAuth\AuthController@register');
      
          Route::get('/admin', 'AdminController@index');
      
      });
      

      编辑 AdminAuth\AuthController.php 文件并添加函数

      protected $redirectTo = '/admin';
      protected $guard = 'admin';
      public function showLoginForm()
      {
          if (view()->exists('auth.authenticate')) {
              return view('auth.authenticate');
          }
      
          return view('admin.auth.login');
      }
      public function showRegistrationForm()
      {
          return view('admin.auth.register');
      }
      

      为管理员创建中间件

      class RedirectIfNotAdmin
      {
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @param  string|null  $guard
       * @return mixed
       */
      public function handle($request, Closure $next, $guard = 'admin')
      {
          if (!Auth::guard($guard)->check()) {
              return redirect('/');
          }
      
          return $next($request);
          }
      }
      

      在内核中注册中间件

       protected $routeMiddleware = [
          'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
      ]; 
      

      在管理控制器中使用这个中间件

      namespace App\Http\Controllers;
      
      use Illuminate\Http\Request;
      
      use App\Http\Requests;
      use App\Http\Controllers\Controller;
      use Illuminate\Support\Facades\Auth;
      
      class AdminController extends Controller
      {
          public function __construct(){
              $this->middleware('admin');
         }
      public function index(){
              return view('admin.dashboard');
          }
      } 
      

      现在你可以像这样使用它了

      Auth::guard('admin')->user()
      

      但不是直接喜欢

      Auth::user()
      

      因为我们有两个身份验证

      【讨论】:

      • 现在一切正常。我可以看到 www.example.com/admin/register 用于创建管理员,并且我可以在路由中看到一个条目。但是对于 www.example.com/register 用户注册正在发生。但是没有post方法可以注册。我猜注册操作是从 AuthController 完成的,但我想知道从哪里调用 AuthController 的 create 函数。
      • 另外请告诉我如何将 adminAuth 用于其他一些控制器。只有这样就够了吗?公共函数 __construct(){ $this->middleware('admin'); }
      猜你喜欢
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 2018-04-25
      相关资源
      最近更新 更多