Redirect::back() возвращает пустой URL в laravel 4

Я хочу перенаправить пользователя обратно на введенный 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();
}

маршруты.php:

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

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

фильтры.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.


person Mohammad LoM    schedule 11.04.2018    source источник


Ответы (2)


Используйте Redirect::intended('/'); вместо Redirect::back();.

P.S. Также меняйте

Route::filter('logged', function($request)
{
    if(!Auth::check())
        return Redirect::guest(URL::to('users')); //Your login form.
});
person Ivanka Todorova    schedule 11.04.2018
comment
Я проверяю это. Он просто перенаправляет пользователя обратно на корневой URL-адрес. Например, если я ввел этот URL-адрес: example.com/settings, после входа в систему я хочу перенаправить обратно на этот URL - person Mohammad LoM; 11.04.2018

используйте метод 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('/');

person lucidlogic    schedule 11.04.2018
comment
Я считаю, что redirect() недоступен в 4.2. - person Ivanka Todorova; 11.04.2018