0

I am trying to load a separate mobile view and having a problem. I can get my mobile layout to work but not the view.

I was using this question as a reference and I am running cakephp 2.1 CakePHP website mobile version

I am not sure how to structure my mobile views?

Is it /app/View/name/mobile/view.ctp or /app/View/mobile/name/view.ctp or something else. I have been going in circles trying to figure this out. Any suggestions.

My AppController.php

Before Filter


    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 );
        }

    }

After Filter (shortened)


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
              );
 }
}   
Community
  • 1
  • 1
Brian Barthold
  • 1,593
  • 4
  • 22
  • 39

2 Answers2

1

In previous version(s) of CakePHP, $this->render() had three parameters, but in 2.x and beyond, it only has 2:

CakePHP 1.3 API for Controller render() - has 3 parameters:

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

CakePHP 2.0 API for Controller render() - has only 2 parameters:

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

Becuase of that, your answer utilizing only 2 parameters works much better than your attempt with 3. :)

(The CakePHP Book still incorrectly states that there are 3 parameters, so - I certainly don't blame you for trying as it's mentioned - had to look it up in more detail to find this out)

Dave
  • 28,833
  • 23
  • 113
  • 183
0

I ended up doing this below. My view folders now check for a mobile folder and load the view if it exists.

    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 );

  }
     }  
Brian Barthold
  • 1,593
  • 4
  • 22
  • 39