Php 我得到;未定义InvalidArgumentException路由[登录];当我在仅限于管理员的管理员路径上注销时
我不明白为什么我会犯这样的错误,但我知道的是:Php 我得到;未定义InvalidArgumentException路由[登录];当我在仅限于管理员的管理员路径上注销时,php,laravel,Php,Laravel,我不明白为什么我会犯这样的错误,但我知道的是: 只有当我在管理员路径上注销时才会发生这种情况/admin 我没有登录路径,我不知道我在哪里使用,为什么使用 未定义的登录路径 如果我将管理中间件组与/admin 从route::group(['middleware'=>['auth']],function(){}到 Route::group(['middleware'=>['web']],function(){}它修复了 问题 我在整个项目中搜索“登录”,但没有找到任何东西 路线相关 不确定如
- 只有当我在管理员路径上注销时才会发生这种情况
/admin
- 我没有登录路径,我不知道我在哪里使用,为什么使用 未定义的登录路径
- 如果我将管理中间件组与
从/admin
到route::group(['middleware'=>['auth']],function(){}
它修复了 问题Route::group(['middleware'=>['web']],function(){}
- 我在整个项目中搜索“登录”,但没有找到任何东西 路线相关
Route::group(['middleware' => ['web']], function(){
Route::get('/signup', 'PagesController@signup')->name('signUpView');
Route::get('/signin', 'PagesController@signin')->name('signInView');
Route::post('/signup', 'UsersController@signUp')->name('signup');
Route::post('/signin', 'UsersController@signIn')->name('signin');
Route::group(['middleware' => ['auth']], function(){
Route::get('/logout', 'UsersController@logOut')->name('logout');
Route::group(['middleware' => 'roles', 'roles' => 'admin'], function(){
Route::get('/admin', 'pagesController@admin')->name('admin');
});
});
});
我用于角色的中间件:
<?php
namespace App\Http\Middleware;
use Closure;
class CheckRole
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->user() === null){
return redirect()->route('home');
}
$actions = $request->route()->getAction();
$roles = isset($actions['roles']) ? $actions['roles'] : null;
if ($request->user()->hasAnyRole($roles) || !$roles ) {
return $next($request);
}
return redirect()->route('home');
}
}
管理员视图:
@extends('layouts.app')
@section('content')
<div class="wrapper">
<form class='uploadForm' action="{{ route('createTag') }}" method="POST" enctype="multipart/form-data">
<label for="tagName">Tag Name</label>
<input class='input' type="text" name="tagName" placeholder="Tag Name">
<input type="file" name="tagImage"> {{ csrf_field() }}
<button class='Submit' type="submit" name="submit">Upload</button>
</form>
</div>
@endsection
@extends('layouts.app'))
@节(“内容”)
标签名
{{csrf_field()}}
上传
@端部
出现这种错误是正常的
为什么?
由于auth中间件,当您注销时,会重定向回您所在的位置,但是最后一个位置(管理员)要求您登录才能继续,这就是为什么中间件会将您重定向到loggin页面,假设您有一个,并且它与命名路径登录
相关
有关更多信息,请参阅:
重定向未经身份验证的用户
当身份验证中间件检测到未经授权的用户时,它将
返回JSON 401响应,如果请求不是AJAX,则返回JSON 401响应
请求时,将用户重定向到名为route的登录名
您可以通过定义未经验证的函数来修改此行为
在您的app/Exceptions/Handler.php
文件中:
use Illuminate\Auth\AuthenticationException;
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest(route('login'));
}
您可以简单地重定向到不需要授权的路由,即
public function logOut(){
Auth::logout();
return redirect()->route('signInView');
}
因此,我只是将具有角色的中间件从“auth”中间件中移出?我还假设在项目文件中的某个地方,默认登录路由名称设置为login by Laravel?我可以将其更改为我的不同名称的登录路由吗?我更新了答案,您可以在其中修改路由更改的名称guest(路由('login'));
到客人(路线('signInView');
你可以走了:)
public function logOut(){
Auth::logout();
return redirect()->route('signInView');
}