2012-11-23 66 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); 

    } 
    }