5

Laravel 4 アプリを作成しています。管理領域を保護して、ユーザーがログイン/認証されている場合にのみアクセスできるようにしたいと考えています。

これを行う最善の方法は何ですか?

Laravel のドキュメントによると、次のようなルートを保護できます。

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

しかし、ルートが次のようになったらどうなるでしょうか。

Route::resource('cms', 'PostsController');

コントローラーに向けられているルートを保護するにはどうすればよいですか?

前もって感謝します!

4

3 に答える 3

18

この目的でルート グループを使用できます。

たとえば、次のようになります。

Route::group(array('before' => 'auth'), function()
{
    Route::get('profile', function()
    {
        // Has Auth Filter
    });

    Route::resource('cms', 'PostsController');

    // You can use Route::resource togehter with 
    // direct routes to the Resource Controller
    // so e.g. Route::post('cms', 'PostsController@save');
});
于 2013-07-07T13:14:22.747 に答える
3

次のように、コントローラーのコンストラクターにフィルターを配置できます。

public function __construct()
    {
        $this->beforeFilter('auth');

        $this->beforeFilter('csrf', array('on' => 'post'));

        $this->afterFilter('log', array('only' =>
                            array('fooAction', 'barAction')));
    }
于 2013-07-07T13:14:31.323 に答える
0

In your PostsController you can put a closure in the constructor to do the same before logic as the previous route.

    public function __construct()
    {
        $this->beforeFilter(function()
        {
            //
        });
    }
于 2013-07-07T13:11:07.810 に答える