2013-07-22 3 views
0

모든 컨트롤러에 대한 언어 설정기를 구현하고 컨트롤러에 대한 라우팅 -> 프런트 컨트롤러 전에이 메서드를 실행해야합니다.Yii 프론트 컨트롤러 구현 전 라우팅

내 컨트롤러 클래스의 메소드를 구현 한 경우

하지만 컨트롤러 전에 이전 실행해야합니다 일부 용도에 대한 initilisation

class Controller extends CController 
{ 
    public function __construct($id, $module = null) 
    { 


    // Set the application language 

    if (isset($_GET['language'])) 
    { 
     $lang = $_GET['language']; 

답변

1

당신은 할 수 응용 프로그램의 onBeginRequest 이벤트를 사용하십시오. 일반적으로 index.php에 코드를 추가해야합니다. 대신 당신은 또한 다른 유효한 callback 첨부 할 수 클로저 기능을 물론

$app = Yii::createWebApplication($config); 
$app->onBeginRequest = function($event) { 
    // ... whatever you want to do 
} 
$app->run(); 

: 여기에 빠른 예입니다.

+0

이전의 __construct 함수 이전에 실행되었습니다. 감사합니다! 감사합니다! – slaver113

-1

당신은 대체 할 수 있습니다 beforeAction ($ 조치)

class Controller extends CController 
{ 
    public function beforeAction($action) 
    { 
    $language = !empty($_GET['lang']) ? $_GET['lang'] : 'en'; 
    return parent::beforeAction($action); 
    } 
} 
+0

beforeAction은 – slaver113