0

I want to implement a language Setter for all Controllers and need to run this method before the Routing to the Controller -> the front Controller.

If have implemented a method in my Controller Class, but for some usages it must be run earlier before controller initilisation

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


    // Set the application language 

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

2 回答 2

1

您可以使用onBeginRequest应用程序的事件。这通常需要您将一些代码添加到您的index.php. 这是一个简单的例子:

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

当然,您也可以附加任何其他有效的回调,而不是闭包函数。

于 2013-07-22T20:56:25.220 回答
-1

您可以覆盖 beforeAction($action)

class Controller extends CController
{
  public function beforeAction($action)
  {
    $language = !empty($_GET['lang']) ? $_GET['lang'] : 'en';
    return parent::beforeAction($action);
  }
}
于 2013-07-22T11:29:52.933 回答