2012-11-23 72 views
0

我正尝试加载单独的移动视图并出现问题。 我可以让我的移动布局工作,但不是视图。cakephp无法加载移动视图

我用这个问题作为一个参考,我正在运行的CakePHP 2.1 CakePHP website mobile version

我不知道如何构建自己的移动的看法?

是/app/View/name/mobile/view.ctp还是 /app/View/mobile/name/view.ctp还是别的。我一直在试图解决这个问题。有什么建议么。

我AppController.php

过滤

 

    public function beforeFilter() {  


     /* mobile layout testing */ 

     if ($this->request->isMobile()){ 
      $this->is_mobile = true; 
       $this->set('is_mobile', true); 
       $this->autoRender = false; 
     } else { 
      $this->set('is_mobile', false); 
     } 

    } 

过滤后前(缩短)

 

function afterFilter() { 
    $view_file = file_exists(
          "/var/www" . 
          $this->webroot . 
          "app" . DS . 
          'View' . DS . 
          $this->name . DS . 
          'mobile/' . 
          $this->action . 
          '.ctp' 
          ); 

$layout_file = file_exists( 
          "/var/www" . 
          $this->webroot . 
          "app" . DS . 
          'View' . DS . 
          'Layouts' . DS . 
          'mobile/' . 
          $this->layout . 
          '.ctp' 
          ); 

if($view_file || $layout_file){   
$this->render(
      $this->action, 
      ($layout_file?'mobile/':'').$this->layout, 
      ($view_file?'mobile/':'').$this->action 
      ); 
} 
} 

回答

1

在CakePHP的前一版本(S),$这个 - >渲染()有三个参数,但是在2.x和超越,它只有2:

的控制器

CakePHP的1.3 API渲染() - 具有3个参数:

http://api13.cakephp.org/class/controller#method-Controllerrender

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

http://api20.cakephp.org/class/controller#method-Controllerrender

的是监守,仅利用2个参数你的答案的作品比3 :)

(CakePHP的图书仍然错误地指出有3个参数,所以你尝试更好 - 我当然不要责怪你尝试,因为它的提及 - 必须更详细地查找它)

+0

谢谢我没有意识到这本书是CakePHP书是不正确的... ... –

0

我结束了下面这样做。我的查看文件夹现在检查移动文件夹并加载视图(如果存在)。

 
    function afterFilter() { 
     // if in mobile mode, check for a valid view and use it 
     if (isset($this->is_mobile) && $this->is_mobile) { 

     $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 : '') . $this->action; 
     $layout_file = ($has_mobile_layout_file ? 'mobile' . DS : '') . $this->layout; 

     $this->render($view_file, $layout_file); 

    } 
    }