Laravel 7:仅管理员角色访问 url 时重定向到域

问题描述

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

'guards' => [
    'web' => [
        'driver' => 'session','provider' => 'users',],'api' => [
        'driver' => 'token','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');
    });

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)