Laravel 5.3身份验证后重定向的自定义路径
我有两种类型的用户:供应商"和客户".而且我目前正在使用Laravel's built-in Auth Controllers
(来自make:auth
命令)来进行客户端用户身份验证.
I have two types of users: "vendors" and "clients". And I'm currently using Laravel's built-in Auth Controllers
(from the make:auth
command) to do my client user authentication.
由于我有两种用户,因此我将LoginController
,RegisterController
和ResetPasswordController
的$redirectTo
属性更改为/client/home
.
And since I have two kinds of users, I have changed the $redirectTo
property on the LoginController
, RegisterController
, and ResetPasswordController
to /client/home
.
这是证据:
RegisterController LoginController
现在,每次我成功完成注册,登录和密码重置后,它都会重定向到/client/home
.
Now, it redirects to /client/home
every time I successfully do registration, login, and password reset.
但是问题是当我已经在mysite.com/client/home
中时,每当我尝试通过地址栏转到mysite.com/register
或mysite.com/login
时,它将重定向到mysite.com/home
而不是mysite.com/client/home
.
But the problem is when I'm in mysite.com/client/home
already, whenever I would try to go to mysite.com/register
or mysite.com/login
via the address bar, it would redirect to mysite.com/home
instead of mysite.com/client/home
...
每当经过身份验证的用户尝试转到/login
或/register
时,如何将其重定向到mysite.com/client/home
?
How can I make it redirect to mysite.com/client/home
whenever an authenticated user tries to go to /login
or /register
?
最简单的选择是为两个登录区域创建单独的控制器.以后将更易于管理,并且您可以更好地自定义行为.
The simplest option is to create separate controllers for both of your login areas. It will be easier to manage later on, and you can customise the behaviour a bit better.
默认文件夹结构如下:
app
|__Http
|__Controllers
|__Auth
|__ForgotPasswordController.php
|__LoginController.php
|__RegisterController.php
|__ResetPasswordController.php
您可以为客户端控制器创建一个附加文件夹,如下所示:
You could create an additional folder for your client controllers, like so:
app
|__Http
|__Controllers
|__Auth
| |__ForgotPasswordController.php
| |__LoginController.php
| |__RegisterController.php
| |__ResetPasswordController.php
|__Client
|__Auth
|__ForgotPasswordController.php
|__LoginController.php
|__RegisterController.php
|__ResetPasswordController.php
这样,您可以分别自定义每个控制器的$redirectTo
属性.
This way you can customise the $redirectTo
properties of each controllers individually.
作为一种替代解决方案,您可以通过在各自的控制器中创建redirectPath
方法来覆盖RedirectsUsers特征的redirectPath
,并返回所需的URL:
As an alternative solution, you could overwrite the redirectPath
of the RedirectsUsers trait, by creating a redirectPath
method in your respective controllers, and return the URL you'd like:
public function redirectPath()
{
if (\Request::is('client/*'))
{
return url('client/home');
}
return url('home');
}
第二个解决方案的优点是您还可以返回控制器操作和命名的路由.我个人不喜欢路由到URL,好像我曾经决定更改它们一样,那么我将不得不在任何地方进行更改.使用控制器动作似乎是一个更好的主意,但是如果以后重构代码,则可能会遇到相同的问题.我更喜欢使用命名路由,因为我可以给它们取一个合理的名称,而不必再更改它们,但仍将所有重定向保持正常工作.
The advantage of this second solution is that you can return controller actions and named routes as well. I personally don't like routing to URLs, as if I ever decide to change them, then I'll have to change them everywhere. Using controller actions seems like a better idea, but you could run into the same problem if you refactor your code later on. I prefer using named routes, as I can give them a sensible name, and never change them again, yet still keep all my redirects in a working order.