CakePHP - 站点离线 - 管理路由不工作 [英] CakePHP - Site Offline - Admin Routing Not Working

查看:200
本文介绍了CakePHP - 站点离线 - 管理路由不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  function beforeFilter(){
// Site Offline = 0,Site Online = 1
if($ this-> ; Configuration-> get_site_status()== 1){
//允许所有用户访问该网站并执行所有必需的操作
// beforeFilter代码
} else {
...
//如果站点为OFFLINE,但用户已登录允许访问。
//稍后我需要将其更改为只允许管理员访问,如果我仍然在开发时登录
//每个人都将被拒绝访问,即使他们能够验证
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'。它默认回到默认布局。



我需要做什么来解决这个问题。

谢谢!

解决方案

我是:

 (!$ 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()数据类型是否等于 type null 。简而言之, null 是数据类型,而不是值。如果你只是在你的if语句中使用=,那么你想使用相同的 === 检查或相同的不检查 !==


I setup the following code in my app_controllers.php file to control access to the site when the site is set to 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('*');
        }
        ...
    }
}

Everything works great when the requested address looks like the following:

http://www.mydomain.com/articles

However, when I have the following it does not work properly

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

It prevents access to the site correctly, but it fails to use the $this->layout = 'offline'. It defaults back to the default layout.

What do I need to do to fix this.

Thank you!

解决方案

Well, the first thing that looks out of place to me is:

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

This looks very wrong and might be causing your problems. I would suggest changing this to something like:

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

or

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

Edits

First, check out the PHP logical operators. You were appending a NOT statement to the return value of $this->Auth->user(). So, with a user logged in you're essentially asking if false is equal to null, which of course it isn't and never will be.

Second, check out the PHP comparison operators. You aren't wanting to check if the value of $this->Auth->user() is equal to the value null, you're wanting to check if the data type of $this->Auth->user() is equal to the type null. In short, null is a data type, not a value. If you did just have to use "=" in your if statement then you would want to use the identical === check or the identical not check !==.

这篇关于CakePHP - 站点离线 - 管理路由不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆