Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP autorender not stopping default view

I'm trying to run a method in a controller that renders the default view on a normal browser, but renders a mobile view when the request is coming from a mobile device.

In app_controller.php

function beforeFilter() { 
    if ($this->RequestHandler->isMobile()) {
        $this->is_mobile = true;
        $this->set('is_mobile', true );
        $this->autoRender = false;
    }
}

and in the controller:

function home(){    
    ...bunch of data grabbing stuff...

    if ($this->is_mobile){
        $this->autoRender = NULL;
        $this->layout = 'empty';
        $this->render('/mobile/home');
    } else {
        $this->layout = 'default';
    }
}

When i hit it on a browser (user agent switched to mobile device) it renders the proper mobile/home view file, BUT it ALSO renders the normal, non-mobile view file underneath. Turned on debug, nothing out of the ordinary, except the 2nd, 'normal' view file is being rendered underneath the mysql trace from the mobile view.

Any thoughts on how to fully disable the default view from rendering and just showing the mobile?

like image 918
tbthorpe Avatar asked Nov 29 '22 16:11

tbthorpe


1 Answers

CakePHP omits options if they are 'false'; You need to change your code like so:

<?php

$this->autoRender = false;

?>

That should stop the view from rendering;

like image 123
Andrew Senner Avatar answered Dec 04 '22 10:12

Andrew Senner