简体   繁体   English

Laravel 4:登录后重定向用户

[英]Laravel 4: redirect users after login

I have page that not allowed for guests, so if guest visit it automatically redirect to login page, 我的页面不允许来宾使用,因此,如果来宾访问它,页面会自动重定向到登录页面,

the question is, Is there any simple way (included in Laravel) to return him back to first page after logged in successfully? 问题是,是否有任何简单的方法(包含在Laravel中)成功登录后将其返回首页?

I know I can use with() in first redirection: 我知道我可以在第一次重定向中使用with():

Redirect::action('UserController@login')->with('url', $_SERVER['REQUEST_URI']);
or
Redirect::action('UserController@login')->withUrl($_SERVER['REQUEST_URI']);

then store and use it in login page, 然后在登录页面中存储和使用它,

but is there something already built in Laravel, 但是Laravel已经内置了一些东西

thanks, 谢谢,

Yes, when your user first enters your site, Laravel stores the entry page in the Session, so, as soon as a login is successful, you can: 是的,当您的用户首次进入您的网站时,Laravel将条目页面存储在Session中,因此,一旦登录成功,您就可以:

return Redirect::intended();

Note that this probably only work if keep using 请注意,这可能仅在继续使用时才有效

return Redirect::guest('login');

In your filtered Route. 在您过滤的路线中。 This is the original Laravel code (filters.php) that provides this: 这是提供此功能的原始Laravel代码(filters.php):

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::guest('login');
});

You can also set a default page (just in case the intended is not available at the moment) using: 您还可以使用以下方法设置默认页面(以防万一目前无法使用该页面):

return Redirect::intended('dashboard');

And you can change the original intended by setting a new one using Session : 您可以通过使用Session设置一个新的目标来更改原始目标:

Session::put('url.intended', $newUrl);

As Antonio's answer states, you can use: 正如安东尼奥的答案所述,您可以使用:

return Redirect::intended();

and it will redirect the user to the page the originally intended to go to, but you can pass an optional 'defualt' paramter so if they login at the login page with no intended location, it will redirect them to the default. 它将把用户重定向到最初打算访问的页面,但是您可以传递一个可选的'defualt'参数,这样,如果他们在没有预定位置的登录页面上登录,它将把他们重定向到默认位置。

i.e. return Redirect::intended('dashboard');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM