I want to make a pattern route, to redirect users to login page when they are not logged in.
I searched but couldn't find a solution. as always Laravel's Documentation is useless!
I have this in my filter.php
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::guest('login');
});
Route::filter('auth.basic', function()
{
return Auth::basic();
});
And this route in my routes.php
Route::when('/*', 'auth' );
but It's not working. How can I do that?
Change the route declaration for login like this
Route::get( '/login', array('as' => 'login', 'uses' => 'UserController#getLogin') );
In your filters.php use
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::route('login');
});
So, you can use
Route::when('*', 'auth' );
You can also use
Route::group(array('before' => 'auth'), function ()
{
// Define all routes here with auth
Route::get('my_first_route', function(){ /... });
Route::post('my_second_route', 'MyController#myAction');
});
Related
I just tried Laravel 5 after a time at 4.2..
The docs says it is possible to use 'before' => 'auth' as always, but for me it does not work.
I have no idea whats wrong, I have read the docs, search on internet but seems not to find anything.
My code looks as:
$router->group(['before' => 'auth'], function($router)
{
//
$router->get('admin', function()
{
return View::make('admin.index');
});
//
$router->get('login', function()
{
return View::make('admin.login');
});
});
Anyone can see what I doing wrong here?
In laravel5 filters are removed. Instead you can use middleware classes which are more clean.
In this blog you can read more about the middleware classes and that they're a replacement of filters.
If you want to do it with self written routes you can use this:
Route::group(['middleware' => 'auth'], function()
{
Route::get('admin', function()
{
return View::make('admin.index');
});
Route::->get('login', function()
{
return View::make('admin.login');
});
});
I'm currently trying to figure out the URL routing in Laravel, I have the basic pages working but I'm trying to figure out the best way to distinguish between guests and members.
The aim is to have the index page user splash view for guests and if a user is logged in then use the dashboard view.
Like this:
Route::group(array('before' => 'guest'), function()
{
Route::get('/', function()
{
return View::make('splash');
}));
});
Route::group(array('after' => 'auth'), function()
{
Route::get('/', function()
{
return View::make('dashboard');
}));
});
At the moment this doesn't work as it always seems to try and display the authenticated 'after' => 'auth' page even when a user is not logged in?
If you want to different views for the same route depending on whether logged in or not you just define the route once and in the closure check to see if the user is logged in and then return the relevant view
Route::get('/', function()
{
if (Auth::check())
return View::make('dashboard');
return View::make('splash');
}
Something like this may get you closer to the solution, but it will still not work, there is more work to do then just showing Views. Also, if you want to show different content on route '/' for auth users and guest users, you cannot use filter.
Route::group(array('before' => 'guest'), function()
{
Route::get('login', function()
{
return View::make('users.login');
}));
});
//~ Route::group(array('before' => 'auth'), function()
//~ {
Route::get('/', function()
{
// if user is logged in
if (Auth::check())
return View::make('dashboard');
else
return View::make('splash');
}));
//~ });
Im facing problem using filters in laravel.
Here is the piece of code
Route::get('admin/login', function()
{
return View::make('admin.login');
});
Route::get('admin/logout', function()
{
Auth::logout();
return Redirect::to('admin/login');
});
Route::post('admin/login', function()
{
$userdata = array('username' => Input::get('username'),
'password' => Input::get('password'));
if (Auth::attempt($userdata))
{
return Redirect::to('admin');
}
else
{
return Redirect::to('admin/login')->with('login_errors',true);
}
});
Route::get('admin', array('before' => 'auth', function() {
return Redirect::to('admin');
}));
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::to('admin/login');
});
The problem is when im opening /admin, it is redirecting me to the admin/login (which is good).
Now if I enter the correct username and password, it should take me to /admin page, but it again redirect me back to admin/login page.
I think whenever I want it to redirect me to /admin, the (before auth) filters run and it takes me back to admin/login.
Please Help
According to me you have a redirect loop here:
Route::get('admin', array('before' => 'auth', function() {
return Redirect::to('admin');
}));
You are trying to redirect to the admin page if the auth filter passes but this is the same route.
I have routes like this:
Route::group(array('before' => 'installed'), function() {
Route::group(array('before' => 'auth_admin', 'prefix' => 'admin'), function()
{
Route::group(array('prefix' => 'gag'), function() {
Route::get('/', 'Admin\\GagController#index');
Route::get('delete/{id}','Admin\\GagController#delete');
});
});
});
I need to prevent users from deleting content on my demo application. So I added the following piece of code before my actual routes.
if(App::environment() === 'demo')
{
Route::get('admin/gag/delete/{id}', function() {
die("You can't delete anything on demo application.");
});
}
//Actual routes are at the below.
However, it doesn't work when Route::get('delete/{id}','Admin\\GagController#delete'); is there. Somehow, Laravel ignores my if block and priorities this route. (Although if block is at the top.)
Looks like routes.php parses my if block after routes are parsed.
How can I make it so Laravel prioritizes my demo routes? I just want to restrict access to some features like this.
Ps. I don't want to add all the routes in if blocks. I just want the routes in my if block to be prioritized.
Route filters are better to do this sort of restrictions:
Route::filter('checkDemo', function()
{
if (App::environment() === 'demo')
{
return Redirect::to('home')->withMessage('You can''t delete anything on demo application.');
}
});
And set the filter to your route:
Route::group(array('prefix' => 'gag', 'before' => 'checkDemo'), function()
{
...
});
Or you can filter just that particular route:
Route::get('delete/{id}', array('before' => 'checkDemo', 'uses' => 'Admin\\GagController#delete'));
i have this below route and that can work correctly
Route::get('admin/login', array('as'=>'login', function()
{
return View::make('back_end.login');
}));
app
views
back_end
layouts
index.blade.php
main.blade.php
profile.blade.php
login.blade.php
for admin i have any view for show and i want to grouping that with admin perfix. after this action and use
http://localhost/laravel/public/admin/login
http://localhost/laravel/public/admin/profile
URL i get this error:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
this is my routes:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('login', function()
{
return View::make('back_end.login');
});
Route::get('index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('profile', function()
{
return View::make('back_end.layouts.profile');
});
});
how to fix this routes. please help me
I had the same issue recently. Here is a slimmed down version of the routing that I used, including a catch all. I was routing to controllers, however you can replace that syntax with a function, the rout will be handled the same.
Route::group(array('prefix' => 'admin'), function(){
Route::get('/','AdminController#index');
Route::resource('users', 'UserController');
Route::get('settings','AdminController#settings');
/* Catch all route */
Route::any('{all}', function($uri){
return Redirect::to('admin')
->with('flash_error', "The administration page 'admin/$uri' could not be found.");
})->where('all', '.*');
});
As always, make sure to run composer dump-autoload after updating the routes. This worked successfully for me. You will only need the '/' on the relative 'base' route.
Make changes (add a preceding slash / to each routes inside the admin group) as given below:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('/login', function()
{
return View::make('back_end.login');
});
Route::get('/index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('/profile', function()
{
return View::make('back_end.layouts.profile');
});
});
It should be /login instead of login and same for each one.