Search code examples
phplaravellaravel-4

laravel allowing only authenticated user to access a specific route


I have already read this in the official documentation:

Protecting A Route

Route::get('profile', array('before' => 'auth', function()
{
    // Only authenticated users may enter...
}));

but i couldn't know how to apply it in my case.

This is my routes.php

Route::get('/', function()
{
    return View::make('hello');
});


Route::get('index', function()
{
    return View::make('index');
});
Route::get('restaurants/admins/{id}', 'RestaurantsController@admins');
Route::resource('restaurants', 'RestaurantsController');
Route::post('admins/login', array('uses' => 'AdminsController@login', 'as' => 'admins.login'));
Route::post('admins/changePicutre', array('uses' => 'AdminsController@changePicture', 'as' => 'admins.changePicture'));
Route::resource('admins', 'AdminsController');
Route::resource('waitingtimes', 'WaitingtimesController');
Route::post('restaurants/changePicture', array('uses' => 'RestaurantsController@changePicture', 'as' => 'restaurants.changePicture'));

Route::get('login', function(){
    return View::make('admins.login');
});

I need all the routes to be only for authenticated users except the loing route.

could you help me please?

many thanks


Solution

  • Use the Route::group() in order to protect all the routes inside the group with the filter, like so:

    // All routes in the group are protected, only authed user are allowed to access them
    Route::group(array('before' => 'auth'), function() {
    
        Route::get('restaurants/admins/{id}', 'RestaurantsController@admins');
    
    });
    
    // Login, all users allowed
    Route::get('login', function(){
        return View::make('admins.login');
    });