首页 > 解决方案 > Laravel 7:访问仅管理员角色的 url 时重定向到域

问题描述

在 laravel 7.x 中我有一些问题。我有一个 url:https://domain/manage/home,只有管理员可以使用管理员角色访问此 url,但是当我访问时,它重定向到 https://domain 我不知道它是如何工作的。config/auth.php 中有我的代码:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],

    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

App\Admin.php 中有我的代码:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Admin extends Authenticatable
{
    use Notifiable;

    protected $guard = 'admin';

    protected $table = 'users';

    protected $fillable = [
        'name', 'username', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

App\Http\Controllers\Admin\ManageController.php 中有我的代码:

    <?php

    namespace App\Http\Controllers\Admin;

    use App\TypeProduct;
    use Illuminate\Http\Request;
    use Illuminate\Foundation\Auth\AuthenticatesUsers;
    use Illuminate\Support\Facades\Auth;
    use App\Http\Controllers\Controller;

    class ManageController extends Controller {
        use AuthenticatesUsers;

        public function __construct()
        {
            $this->middleware('auth:admin');
        }

        public function guard()
        {
            return Auth::guard('admin');
        }

        public function index()
        {
            return view('menu/manage/manage_home');
        }
    }

App\Providers\RouteServiceProvider.php 中有我的代码:

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        $this->mapAdminRoutes();
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

    protected function mapAdminRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace . '\Admin')
            ->group(base_path('routes/admin.php'));
    }
}

在 routes\admin.php 中有我的代码:

    <?php

    use Illuminate\Support\Facades\Route;
    use Illuminate\Support\Facades\App;

    Route::prefix('manage')->group(function () {
        Route::get('/home', 'ManageController@index')->name('manage.index');
    });

标签: laravelroutesmiddlewarelaravel-7guard

解决方案


推荐阅读