laravel 5.3 自定义登陆成功后的跳转路由无效?

2017-01-08 21:41:19 +08:00
 xx19941215

在官方文档中说道

If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property:

下面是我 LoginController 中的代码:

<?php

namespace App\Http\Controllers\User\Ui\Auth;

use App\Http\Controllers\Controller as Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Redirect;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
//    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    public function index()
    {
        return \Auth::user();
    }

    protected function redirectTo()
    {
        die('ok');
        //return Redirect::route('index');
    }
}

但是登录成功收任然跳转到 /home

这是为啥?

5576 次点击
所在节点    PHP
14 条回复
xx19941215
2017-01-08 21:56:17 +08:00
大 phper 在哪呢?
wkan
2017-01-08 22:12:11 +08:00
你开一个隐身窗口(排除 session 影响),然后直接打开登录页,登录试试?应该是从别的页面跳转到登录页的时候会在 session 里记录一个来源页的 url ,有这个 url 的时候是不会用这个 redirectTo 方法获取跳转目标 url 的
crystom
2017-01-08 22:13:32 +08:00
protected $redirectTo='/your/path';
xx19941215
2017-01-08 22:14:03 +08:00
@wkan 我试试哈
crystom
2017-01-08 22:14:48 +08:00
不好意思 看错了 你应该复写 redirectPath 方法
xx19941215
2017-01-08 22:16:24 +08:00
@wkan 在隐身模式还是登录还是跳转到了 'your/path/home' 。。。
anviod
2017-01-08 22:21:11 +08:00
默认不给参数 就是自动跳转到 /home
去看 Illuminate\Foundation\Auth\RedirectsUsers;
第十八行

return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
wkan
2017-01-08 22:26:08 +08:00
@xx19941215 是 redirectPath() 记错方法了。。
https://ooo.0o0.ooo/2017/01/08/58724bb32453b.png
不过 seesion 里的 url 确实会影响到跳转位置,要在这个方法里去分不同的跳转的位置要注意一下
xx19941215
2017-01-08 22:37:59 +08:00
@anviod 可是文档说 写一个 redirectTo 方法就可以,为什么我这里不生效? redirectPath 重写还是可以的,难道是文档错了?
xx19941215
2017-01-08 22:44:43 +08:00
@crystom 恩恩,改这个确实有用。我之前也试过,但是不清楚为什么文档里写的不好使。下面是我重写的 redirectPath()方法。

```php
public function redirectPath()
{
return redirect(route('index'));
}
```

出现了如下问题截图
![]( https://ooo.0o0.ooo/2017/01/08/58724fe76c342.png)

我跳转的地址和登录的地址不在一个二级域下(跳转到首页,登录在 login 二级域下)
crystom
2017-01-08 22:50:14 +08:00
@xx19941215 返回一个 url 字符串即可,比如 return route('index');
xx19941215
2017-01-08 22:55:41 +08:00
@crystom 恩,直接返回 url 字符串不出错了,但是登录之后页面刷新,还是 login.domian.com 。。。。一脸懵逼
crystom
2017-01-08 22:59:36 +08:00
@xx19941215 一个比较拙劣的方式 return 'http://redirect.domain.com/yourpath';
anviod
2017-01-10 12:00:17 +08:00
@xx19941215
<?php

namespace Illuminate\Foundation\Auth;

trait RedirectsUsers
{
/**
* Get the post register / login redirect path.
*
* @return string
*/
public function redirectPath()
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}

return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
}

若自定义 redirectTo 成员函数 需要返回值为 string 类型。
可以这样写
protected function redirectTo()
{
// die('ok');
return '/index';
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/333138

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX