Keith Power
Keith Power

Reputation: 14151

Cakephp multiple views per controller

I have a controller which has 3 functions. I wish to show 3 different views and layouts in each function depending on whether the user comes from mobile, website or facebook. I am passing in already where the user is coming from.

I am unsure how I would then show a particular view and layout for each. Here is some code that I started to do to change the layout. I have the views in a folder called res.

function availability() {

    if ($_REQUEST['from'] == 'facebook') {
        $this->layout = 'facebook';
        print_r ('face');
    }elseif ($_REQUEST['from'] == 'website'){
        $this->layout = 'website';
        print_r ('web');
    }elseif ($_REQUEST['from'] == 'mobile'){
        $this->layout = 'mobile';
        print_r ('mobile');         
    };
}

Upvotes: 3

Views: 2059

Answers (1)

JJJ
JJJ

Reputation: 33153

Use $this->render() to change the view.

$this->layout = 'facebook';
$this->render( 'res/facebook' );

You could also put all the views for different layouts to their own folders and set the viewpath so that you don't have to choose the views manually in each function:

function beforeFilter() {
    parent::beforeFilter();
    $this->viewPath = $_REQUEST[ 'from' ];
}

Now the view for action "availability" for the Facebook layout is fetched from facebook/availability.ctp.

Upvotes: 4

Related Questions