Cakephp移动布局主页重定向.(RequestHandler->;isMobile())


Cakephp Mobile layout Home redirect. (RequestHandler->isMobile() )

我正在尝试构建我的网站的移动版本usign Cakephp和JQueyrMobile。对于Cakephp部分,我选择在app_controller中使用以下内容:

    if($this->RequestHandler->isMobile() || isset($this->params['url']['mobile'])) {
       $this->layout = 'mobile';
       $this->isMobile = true;
    }
    $this->set('is_mobile', $this->isMobile);
    if($this->isMobile) {
           $this->action = 'mobile/'.$this->action;
     }

感谢我为每个控制器/视图创建/mobile/文件夹,它运行良好。我只是对home.ctp有问题!我在/views/pages/mobile/home.ctp中创建了另一个移动版本,但它加载了/views/pages/home.ctp。

我应该怎么做才能重定向到移动版的home.ctp usingn上面的代码?谢谢

您是否尝试在AppController中使用beforeFilterafterFilter

我把它用于我的一个Cake应用程序的移动版本:

  function beforeFilter() {
    if ($this->RequestHandler->isMobile()) {
      $this->is_mobile = true;
      $this->set('is_mobile', true );
      $this->autoRender = false;
    }
  }

和后过滤器:

  function afterFilter() {
    if (isset($this->is_mobile) && $this->is_mobile) {
      $view_file = file_exists( VIEWS . $this->name . DS . 'mobile/' . $this->action . '.ctp' );
      $layout_file = file_exists( LAYOUTS . 'mobile/' . $this->layout . '.ctp' );
      $this->render($this->action, ($layout_file?'mobile/':'').$this->layout, ($view_file?'mobile/':'').$this->action);
    }
  }

此代码来自Cake中关于移动内容的另一个问题,请查看此答案以了解更多信息。同一问题的另一个答案涉及布局。