Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');         
    };
}
like image 256
Keith Power Avatar asked Jan 21 '26 11:01

Keith Power


1 Answers

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.

like image 161
JJJ Avatar answered Jan 23 '26 20:01

JJJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!