CakePHP -站点离线-管理路由不工作



我在我的app_controllers.php文件中设置了以下代码,以控制当站点设置为OFFLINE (site_status = 0)时对站点的访问。

function beforeFilter(){
    // Site Offline = 0 , Site Online = 1
    if($this->Configuration->get_site_status() == 1){
          // Allow access to the site to all users and perform all required 
          // beforeFilter code
    }else{
        ...
        // If site is OFFLINE but User is logged in allow access. 
        // Later I will need to change it to only allow admin access if logged in as I am still developing
        // Everyone else will be denied access even if they are able to authenticate          
        if(!$this->Auth->user() == null){
            $this->layout = 'default';
            $this->Auth->allow('*');
        }else{        
            $this->layout = 'offline';
            $this->Auth->deny('*');
        }
        ...
    }
}

当请求的地址看起来像下面这样时,一切都工作得很好:

http://www.mydomain.com/articles

然而,当我有以下内容时,它不能正常工作

http://www.mydomain.com/admin/articles

阻止正确访问站点,但无法使用$this->layout = 'offline'。默认为"default"布局。

我需要做些什么来修复这个

谢谢!

您的if条件看起来很奇怪。它们是:

If site is offline and user logged in
    use default layout
otherwise
    use offline layout and require authentication on all pages

。当网站在线或用户未登录时,您正在使用离线布局。你确定这是你想要的吗?

嗯,对我来说,第一个看起来不合适的东西是:

(!$this->Auth->user() == null)

这看起来很不对,可能会导致你的问题。我建议将其更改为:

(!is_null($this->Auth->user())

($this->Auth->user() !== NULL)

编辑

首先,检查PHP逻辑操作符。您将NOT语句附加到$this->Auth->user()的返回值。所以,当一个用户登录时,你实际上是在问false是否等于null,当然它不是,也永远不会是。

第二,检查PHP比较操作符。您不想检查$this->Auth->user()是否等于null的值,您想检查$this->Auth->user()数据类型是否等于类型 null。简而言之,null是一个数据类型,而不是一个值。如果你只需要在If语句中使用"=",那么你会想要使用相同的===检查或相同的不检查!==

最新更新