Laravel使资源仅对来宾用户可用


Laravel make a resource available to only guest users

这是我的路线:

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

我希望restaurants.create对来宾用户可用。

这可能吗?

就像这样声明Route Group外的路由:

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

而不是:

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

然后在RestaurantsController控制器中添加before过滤器在__construct方法中,像这样:

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

所以所有的方法将auth作为before过滤器,但没有create方法。如果你需要添加另一个before过滤器,那么你可以在第一个过滤器之后添加另一个,像这样:

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')));
}

我相信您正在寻找guest过滤器,默认情况下应该包含它。

所以把这行改成:

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

:

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