2010-09-08 84 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