Php 尝试重定向时出现laravel 6路线问题
当有人在URL中点击主页时,我试图重定向到登录页面,但它总是给出类似这样的错误 未定义路由[管理员/登录]。 同一个问题存在许多问题,但不能解决问题 如果直接输入URL,同样的路由也可以工作,但是从Php 尝试重定向时出现laravel 6路线问题,php,laravel,laravel-6,laravel-6.2,Php,Laravel,Laravel 6,Laravel 6.2,当有人在URL中点击主页时,我试图重定向到登录页面,但它总是给出类似这样的错误 未定义路由[管理员/登录]。 同一个问题存在许多问题,但不能解决问题 如果直接输入URL,同样的路由也可以工作,但是从Authenticate.php重定向不起作用 路由/web.php Route::get('/', function () { return view('welcome'); }); Auth::routes(); // Admin Routes // Without auth Rout
Authenticate.php
重定向不起作用
路由/web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
// Admin Routes
// Without auth
Route::group(['prefix' => 'admin', 'namespace' => 'Auth'], function () {
Route::get('/login', 'AdminLoginController@login');
});
Route::group(['prefix' => 'admin', 'namespace' => 'Auth', 'middleware' => 'auth:admin'], function () {
Route::get('/home', 'AdminLoginController@home');
});
Authenticate.php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
if ($request->is('admin') || $request->is('admin/*')) {
return route('admin/login');
} else if ($request->is('vendor') || $request->is('vendor/*')) {
return route('vendor/login');
} else {
return route('login');
}
}
}
}
您不命名路线,因此不能这样称呼它们,您需要使用:
Route::group(['prefix' => 'admin', 'namespace' => 'Auth'], function () {
// just add the name to the route to call route('login')
Route::get('/login', 'AdminLoginController@login')->name('login');
});
然后你可以打电话:
return route('login');
或者,如果您不想命名路线,请改用:
return redirect('admin/login');
编辑:
我的错误是,您使用了redirectTo函数,所以您只需要返回一个字符串,使用以下命令:
return 'admin/login';
您不命名路线,因此不能这样称呼它们,您需要使用:
Route::group(['prefix' => 'admin', 'namespace' => 'Auth'], function () {
// just add the name to the route to call route('login')
Route::get('/login', 'AdminLoginController@login')->name('login');
});
然后你可以打电话:
return route('login');
或者,如果您不想命名路线,请改用:
return redirect('admin/login');
编辑:
我的错误是,您使用了redirectTo函数,所以您只需要返回一个字符串,使用以下命令:
return 'admin/login';
我尝试了
返回重定向('admin/login')
但它给出了ErrorException标头可能只包含一个标头,新行检测查看我的编辑,我没有看到您正在使用redirectTo()函数,在这种情况下只返回一个字符串,但您应该使用路由名称,然后使用路由()
函数,它返回一个字符串,这样您可以更灵活地使用URI。如果答案是肯定的,请接受。是的,我对laravel很陌生,所以我正在学习,请你给我一些关于多重身份登录的建议,就像我有多重登录,比如管理员、供应商、用户。因此,我正在使用的方法和我必须更改的任何内容,请建议我。我尝试了返回重定向('admin/login')
但它给出了ErrorException标头可能只包含一个标头,新行检测查看我的编辑,我没有看到您正在使用redirectTo()函数,在这种情况下只返回一个字符串,但您应该使用路由名称,然后使用路由()
函数,它返回一个字符串,这样您可以更灵活地使用URI。如果答案是肯定的,请接受。是的,我对laravel很陌生,所以我正在学习,请你给我一些关于多重身份登录的建议,就像我有多重登录,比如管理员、供应商、用户。所以,我正在使用的方法和任何我必须改变的东西,请建议我。