【问题标题】:Redirect::back() returning an empty url in laravel 4Redirect::back() 在 laravel 4 中返回一个空 url
【发布时间】:2018-04-11 13:42:32
【问题描述】:

我想在验证用户后将用户重定向回输入的 url 场景是:用户输入一个 url,在后端我检查用户是否经过身份验证,如果用户未通过身份验证,则将用户重定向到登录 url,成功登录后将用户重定向回用户输入的第一个 url

用户控制器:

public function index()
{
    if(!Auth::check())
    {
        return View::make('adminLogin');
    }
    return Redirect::to('admin');
}

public function login() 
{
    $creds = array(
        'username' => Input::get('username'),
        'password' => Input::get('password')
        );

    if (Auth::attempt($creds))
        return Redirect::back();
}

routes.php:

Route::resource('users', 'UsersController');

Route::post('login', 'UsersController@login');

filters.php:

Route::filter('logged', function($request)
{
    if(!Auth::check())
        return Redirect::to('users');
});

设置控制器.php:

public function __construct()
{
    $this->beforeFilter('logged', array('only' => array('index')));
}

public function index()
{
    return View::make('setting');
}

请帮助我。我是 Laravel 的新手。

【问题讨论】:

    标签: laravel laravel-4.2


    【解决方案1】:

    使用Redirect::intended('/'); 而不是Redirect::back();

    PS:还要改

    Route::filter('logged', function($request)
    {
        if(!Auth::check())
            return Redirect::guest(URL::to('users')); //Your login form.
    });
    

    【讨论】:

    • 我测试了一下。它只是将用户重定向回root url。例如,如果我输入这个url:example.com/settings,登录后我想重定向回这个url
    【解决方案2】:

    使用intended() 方法

    The intended method on the redirector will redirect the user to the URL they were attempting to access before being intercepted by the authentication middleware. A fallback URI may be given to this method in case the intended destination is not available.

    示例:return Redirect::intended('/');

    【讨论】:

    • 我相信 redirect() 在 4.2 中不可用。
    猜你喜欢
    • 2016-08-31
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    相关资源
    最近更新 更多