2010-09-08 131 views
1

嘗試在控制器內部使用__construct來分配一些變量,但它一直拋出錯誤。希望有人能帶領我走向正確的方向。Kohana 3控制器構造

class Controller_Mobile extends Controller { 

    public function __construct() 
    { 
     parent::__construct();  

     $iphoneDetect = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone"); 
     $touchDetect = strpos($_SERVER['HTTP_USER_AGENT'],"iPod"); 
     $blackberry = strpos ($_SERVER['HTTP_USER_AGENT'], 'blackberry'); 
     $android = strpos ($_SERVER['HTTP_USER_AGENT'], 'android'); 

     $iphoneDetect = true; 
     if ($iphoneDetect == true || $touchDetect == true) 
     { 
      $directory = "mobile/iphone"; 
     } 
     else if($android == true) 
     { 
      $directory = "mobile/android"; 
     } 

    } 
    public function action_index() 
    { 
     $this->request->response = 'I am mobile'; 
    } 
+0

什麼是錯誤? – irishbuzz 2010-09-08 19:58:03

回答

3

如果你想使用__construct()方法,不要忘了Request變量。

+0

Downvote,因爲@ shyammtp的答案更完整。 – 2013-11-08 14:08:52

+1

請注意,$答覆參數在Kohana v3.1.0中添加,差不多1年後通過回答。 – biakaveron 2014-01-15 04:58:19

7

我其實只是找到了問題的答案,只是想我會通過它。在Kohana 3中,你使用before()和after()函數。爲什麼你得到錯誤,你的代碼

public function __construct(Kohana_Request $request) 
{ 
    parent::__construct($request); 
    // your code 
} 

這就是:

+0

所以而不是:__construct()你說我可以使用before()?謝謝和順便說一句,構造函數有2個參數:public function __construct(Request $ request,Response $ response) – 2011-05-10 15:56:32

+0

此答案對任何版本都是正確的。 – biakaveron 2014-01-15 04:59:39

5

您必須在構造中同時使用請求和響應。

public function __construct(Request $request, Response $response) 
{ 
    parent::__construct($request,$response); 
    // your code 
} 
+2

由於Kohana 3.1.0。 – biakaveron 2014-01-15 04:59:06