0

I've just installed Laravel in my pc and I have a problem with routing. When a user type the URL http://laravel.app/admin without logging in, I want to redirect it to http://laravel.app/login?Return_URL=http://laravel.app/admin just what Jeffrey Way did to Laracast.

How can I do it?

I already added this to my routes.php

Route::get('/admin', array('before' => 'auth', 'as' => 'admin.index', 'uses' => 'AdminController@index'));

And I don't know what to do next.

=== CLARIFICATION ===

When a user went to a protected pages which is /admin or /admin/{{other-page}} I want it to be redirected to /login but with the parameter of URL in which where it went before he was redirected to login page. After successfully logged in, the user will then be redirected to the first URL he intended to go to which is captured in the ?Return_URL= parameter.

3 Answers 3

1

You should have a filters file located at app/filters.php. If you open it up you should see this something like this:

Route::filter('auth', function() { }); 

This is the function that triggers before the route is served. Inside here you can redirect if they are not logged in. For example:

Route::filter('auth', function() { if (!Auth::check()) { // The user is not logged in so redirect them } }); 

For more information checkout the security section and the routing section of the docs.

Sign up to request clarification or add additional context in comments.

2 Comments

I already opened that file. But you miss my point. I want to redirect user after logging in to the URL he was in before he was redirected to login as I applied filter auth
Ok your question wasn't clear that is what you wanted
1

Why not using Redirect::intended('fallback')? This redirects the user back to the page he was intended to look. You can also give a fallback url.

You can read more about it here.

EDIT: To be more specific you can do it like below:

Route::group(array('prefix' => 'admin','before' => 'auth'),function(){ //here you're admin routes }); 

In the filters file you change the auth filter to something like:

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

The guest method make it so that the intended url is saved in the session. On the login page you can do this after you've attempted the users credentials:

Route::intended('fallback'); 

But when you really want to do it with a query parameter, just do this:

Route::filter('auth',function(){ if(!Auth::check()) return Redirect::to('login',array('Return_URL' => Route::current())); }); 

3 Comments

This appears to be the only answer that promotes the best practice. However, I feel that you should maybe bulk your answer out (link-only answers are asking for trouble) and also make mention of Redirect::guest() which goes with Redirect::intended().
So you mean I have to make tons of Redirect::intended('fallback') since there is a possibility that the user also type the link /admin/show/ and others which is also protected page?
@alexrssell you're right. I updated the answer with more information.
0

You could modify your 'auth' filter. If the user is not logged in, redirect to a login page.

Route::filter('auth', function(){ if Auth::guest(){ // sign them in } else{ return Redirect::to('pathToLogin'); } }; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.