laravel - 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');
});
解决方案
推荐阅读
- node.js - request-promise 无法使用 resolveWithFullResponse: true 找到状态代码
- html - 如何修复使用 Mozilla Firefox 打印时被切断的页面
- php - 如何在共享 IP 中使用 websocket,连接建立时出错:net::ERR_CERT_AUTHORITY_INVALID?
- javascript - jQuery/Bootstrap 跟随 div 悬停项
- javascript - 带有导航(下一个、上一个)按钮的 CSS 滚动捕捉点
- c++ - 在 Eclipse 中获取临时地址时出错
- r - `sjPlot::tab_df()`--如何设置小数位数?
- python - 公式在 IEEE 754 中失败的概率
- python - 在循环数据帧中转置系列
- c# - NewtonSoft 反序列化嵌套的 JSON Blue Prism