cakepp无法加载移动视图



我试图加载一个单独的移动视图,但遇到了问题。我可以让我的移动布局工作,但不能查看视图。

我用这个问题作为参考,我正在运行cakehp2.1CakePHP网站移动版

我不知道如何构建我的移动视图?

是/app/View/name/mobile/View.ctp还是/app/View/mobile/name/View.ctp或其他内容。我一直在兜圈子试图弄清楚这一点。任何建议。

我的AppController.php

过滤前

公共函数beforeFilter(){/*移动布局测试*/if($this->request->isMobile()){$this->is_mobile=true;$this->set('is_mobile',true);$this->autoRender=false;}其他{$this->set('is_mobile',false);}}

后过滤器(缩短)

函数afterFilter(){$view_file=file_exists("/var/www"。$this->webroot。"应用程序"。DS。"查看"。DS。$this->名称。DS。'mobile/'。$this->操作。'.ctp');$layout_file=file_exists("/var/www"。$this->webroot。"应用程序"。DS。"查看"。DS。"布局"。DS。'mobile/'。$this->布局。'.ctp');if($view_file||$layout_file){$this->渲染($this->操作,($layout_file?'mobile/':'')$此->布局,($view_file?'mobile/':'')$此->操作);}}

在CakePHP的早期版本中,$this->render()有三个参数,但在2.x及更高版本中,它只有2:

CakePHP 1.3控制器渲染API()-有3个参数:

http://api13.cakephp.org/class/controller#method-控制器

CakePHP 2.0 API控制器渲染()-只有2个参数:

http://api20.cakephp.org/class/controller#method-控制器

因此,只使用2个参数的答案比使用3个参数的尝试效果要好得多。:)

(CakePHP书仍然错误地指出有3个参数,所以——我当然不责怪你像上面提到的那样尝试——必须更详细地查找它才能找到答案)

我最终完成了下面的操作。我的视图文件夹现在检查移动文件夹并加载视图(如果存在)。

函数afterFilter(){//如果处于移动模式,请检查有效视图并使用它if(isset($this->is_mobile)&&$this->is_mobil){$has_mobile_view_file=file_exists(ROOT.DS.APP_DIR.DS.'view'.DS.$this->name.DS.'mobile'.DS.$this->action.'.ctp');$has_mobile_layout_file=file_exists(ROOT.DS.APP_DIR.DS.'View'.DS.'Layouts'.DS.'mobile'.DS.$this->layout.'.ctp');$view_file=($has_mobile_view_file?'mobile'.DS:")$这个->动作;$layout_file=($has_mobile_layout_file?'mobile'.DS:")$此->布局;$this->render($view_file,$layout_file);}}

最新更新