V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xx19941215
V2EX  ›  PHP

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

  •  1
     
  •   xx19941215 · 2017-01-08 21:41:19 +08:00 · 5566 次点击
    这是一个创建于 2666 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在官方文档中说道

    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

    这是为啥?

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

    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
    wkan
        8
    wkan  
       2017-01-08 22:26:08 +08:00
    @xx19941215 是 redirectPath() 记错方法了。。
    https://ooo.0o0.ooo/2017/01/08/58724bb32453b.png
    不过 seesion 里的 url 确实会影响到跳转位置,要在这个方法里去分不同的跳转的位置要注意一下
    xx19941215
        9
    xx19941215  
    OP
       2017-01-08 22:37:59 +08:00
    @anviod 可是文档说 写一个 redirectTo 方法就可以,为什么我这里不生效? redirectPath 重写还是可以的,难道是文档错了?
    xx19941215
        10
    xx19941215  
    OP
       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
        11
    crystom  
       2017-01-08 22:50:14 +08:00
    @xx19941215 返回一个 url 字符串即可,比如 return route('index');
    xx19941215
        12
    xx19941215  
    OP
       2017-01-08 22:55:41 +08:00
    @crystom 恩,直接返回 url 字符串不出错了,但是登录之后页面刷新,还是 login.domian.com 。。。。一脸懵逼
    crystom
        13
    crystom  
       2017-01-08 22:59:36 +08:00
    @xx19941215 一个比较拙劣的方式 return 'http://redirect.domain.com/yourpath';
    anviod
        14
    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';
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4851 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 05:38 · PVG 13:38 · LAX 22:38 · JFK 01:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.