2013-07-07 2 views
5

Laravel 4 앱을 구축 중이며 관리자 영역을 보호하여 사용자가 로그인/인증 된 경우에만 액세스 할 수 있도록하고 싶습니다.Laravel 4 : 컨트롤러가 제공하는 경로 보호

가장 좋은 방법은 무엇입니까?

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

하지만 어떻게하면 내 경로는 다음과 같습니다 때 :

Laravel 워드 프로세서는이 같은 경로를 보호 할 수 있다고

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

을 나는에 지시되는 경로를 보호하려면 어떻게 컨트롤러? 사전에

감사합니다!

답변

18

이 목적으로 Route Groups을 사용할 수 있습니다. 당신은이처럼 컨트롤러의 생성자에 필터를 넣을 수 있습니다

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', '[email protected]'); 
}); 
+1

안녕 얘들 아, 이것은 쉽게 답을 구현했다 : Route :: group (array ('before'= 'auth'), function() { Route :: resource ('cms', 'PostsController'); }); 감사합니다. – Josh

+0

@Josh Glad 그것이 도움이되었습니다. –

0

PostsController에서 이전 경로와 같은 로직을 수행하기 위해 클로저를 생성자에 넣을 수 있습니다.

public function __construct() 
    { 
     $this->beforeFilter(function() 
     { 
      // 
     }); 
    } 
3

: 그래서 예를 들면

- 나는에만 필요하지만

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

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

     $this->afterFilter('log', array('only' => 
          array('fooAction', 'barAction'))); 
    } 
+0

그래서 특정 HTTP 메소드에만 필터를 적용 할 수 있습니까? 지금 테스트 중 ... –

+0

가능합니다. http://laravel.com/docs/controllers#controller-filters – Melvin