php CakePHP 自动渲染不会停止默认视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15663244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:42:23  来源:igfitidea点击:

CakePHP autorender not stopping default view

phpcakephpcakephp-1.3

提问by tbthorpe

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

在 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.

当我在浏览器上点击它(用户代理切换到移动设备)时,它会呈现正确的移动/主页视图文件,但它也会在下面呈现正常的非移动视图文件。打开调试,除了第二个“普通”视图文件在移动视图的 mysql 跟踪下方呈现之外,没有任何异常。

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

关于如何从渲染中完全禁用默认视图并仅显示移动设备的任何想法?

回答by Andrew Senner

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

如果选项为 'false',CakePHP 会忽略选项;您需要像这样更改代码:

<?php

$this->autoRender = false;

?>

That should stop the view from rendering;

这应该会停止渲染视图;

回答by Naresh Dudhat

if you want to make autorender off for specific view then add

如果要为特定视图关闭自动渲染,请添加

$this->autoRender = false;

in a specific method rather then in app controller

在特定方法中而不是在应用程序控制器中