Anastasie Laurent
Anastasie Laurent

Reputation: 1179

Laravel make a resource available to only guest users

This is my route:

Route::group(array('before' => 'auth'), function() {

    Route::resource('restaurants', 'RestaurantsController');

});

I want the restaurants.create to be available for guest users.

Is that possible?

Upvotes: 0

Views: 1215

Answers (2)

The Alpha
The Alpha

Reputation: 146191

Just declare the route outside of the Route Group like this:

Route::resource('restaurants', 'RestaurantsController');

Instead of this:

Route::group(array('before' => 'auth'), function() {
    Route::resource('restaurants', 'RestaurantsController');
});

Then in your RestaurantsController controller add the before filter inside the __construct method like this:

public function __construct()
{
    parent::__construct(); // If BaseController has a __construct method
    $this->beforeFilter('auth', array('except' => array('create')));
}

So all the methods will have auth as the before filter but without the create method. If you need to add another before filter then you may add another one just after the first one like this:

public function __construct()
{
    parent::__construct(); // If BaseController has a __construct method

    // Add auth as before filter for all methods but not for create
    $this->beforeFilter('auth', array('except' => array('create')));

    // Only for create method
    $this->beforeFilter('anotherFilter', array('only' => array('create')));
}

Upvotes: 1

Evan Darwin
Evan Darwin

Reputation: 860

I believe you're looking for the guest filter, which should be included by default.

So change the line from:

Route::group(array('before' => 'auth'), function() {

to:

Route::group(array('before' => 'guest'), function() {

Upvotes: 1

Related Questions