Redirect Laravel身份验证自定义路由重定向到/登录而不是/(自定义路径)/登录

Redirect Laravel身份验证自定义路由重定向到/登录而不是/(自定义路径)/登录,redirect,laravel-5.3,Redirect,Laravel 5.3,我使用的是Laravel 5.3.29。我想为身份验证创建一个自定义路由 这是我的网络路线代码 Route::group(['prefix' => 'hrs'], function(){ Auth::routes(); }); Route::get('/', function () { return view('welcome'); }); Route::get('/home', 'HomeController@index'); 我在/home和/hrs/home之间对所有资源代码进行了

我使用的是Laravel 5.3.29。我想为身份验证创建一个自定义路由

这是我的网络路线代码

Route::group(['prefix' => 'hrs'], function(){ Auth::routes(); });
Route::get('/', function () { return view('welcome'); });
Route::get('/home', 'HomeController@index');
我在/home和/hrs/home之间对所有资源代码进行了url更改

这是我的家庭控制器代码:

public function __construct()
{ $this->middleware('auth'); }
public function index()
{ return view('home'); }
问题发生在我未登录时点击localhost/home时。它将我重定向到localhost/login,而不是localhost/hrs/login。


我必须在哪里更改才能重定向到localhost/hrs/login

除了一件事,你什么都做得很好。您尚未将
Handler.php
文件更改为
app\Exceptions\
目录。只需将
Handler.php
文件更改为
unauthenticated()
方法,如下所示:

return redirect()->guest('hrs/login')

完整代码:

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest('hrs/login');
}

这对我有帮助。在自定义控制器中,您可以使用受保护的$redirectTo属性,也可以使用方法受保护的函数redirectTo() 在本例中,我假设您希望重定向到主页。可以使用所需的管线和视图

这是我的密码

示例1:只需设置属性

protected $redirectTo = 'home';
示例2:使用硬编码路径

protected function redirectTo(){

    return '/home';

}
示例3:如果定义了路由,则可以重定向到某个路由

protected function redirectTo(){

    return route('home');

}
帮我解决了这个问题