加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

cakephp configure :: read value is lost’尝试过常量,同样的行

发布时间:2020-12-13 17:26:32 所属栏目:PHP教程 来源:网络整理
导读:我的应用程序从Pages controlleR开始;应用程序需要的第一件事是设置一个配置值-from url-我想在整个应用程序上使用,例如localhost / laborbase / client1我需要将’client1’保持为常量. 所以在AppController中,beforefilter,我分配一个常量(尝试使用Configu
我的应用程序从Pages controlleR开始;应用程序需要的第一件事是设置一个配置值-from url-我想在整个应用程序上使用,例如localhost / laborbase / client1我需要将’client1’保持为常量.

所以在AppController中,beforefilter,我分配一个常量(尝试使用Configure ::但同样的问题:其他控制器的值丢失了.

AppController的

public function beforeFilter() {
    $clienturl = explode('/',$_SERVER['REQUEST_URI']);
     if (sizeOf($clienturl) == 3) {
        $this->__fetchSettings($clienturl[2]);
    }

public function __fetchSettings($value) {
    debug('from app'.$value);
    //Configure::write('CLIENT_URL',$value);
    define("CLIENT_URL",$value);
    debug('after assign:'.CLIENT_URL); // THIS SHOWS FINE
}

PagesControler:

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('home','homedemo');
}

UsersController:

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('clientLogin','add');
}

并在调用页面操作时显示测试值(页面/显示

public function display() {
        debug(Configure::read('CLIENT_URL')); // IT SHOWS FINE HERE
        ...

从UsersController,我无法访问值:常量未定义

public function clientLogin($id = null) {
    $this->autoRender = false;
    $this->RequestHandler->setContent('json','application/json');
    ob_end_clean();
    ///debug(Configure::read('CLIENT_URL'));
    echo json_encode(CLIENT_URL);
}

其他控制器无法使用.尝试配置::同样的事情.

我需要能够从我的应用程序中的任何位置访问配置值.

你能帮我吗?

解决方法

您基本上希望从第一个请求中获取操作,并将其存储在常量中.你可以这样做,当然 – 但常量只在当前请求期间持续,所以你需要使用一个会话来存储下一个请求的值:

您可以将AppController的类属性共享给所有其他控制器,因为它们都扩展了AppController,或者您可以使用类似于当前的常量.我将在此示例中使用类属性.

您需要在AppController中设置属性,并使用Session component:

var $components = array('Session');
public $clienturl = null;

然后你的AppController beforeFilter:

function beforeFilter() {
    // use if(!defined('CLIENT_URL')) for constants
    if(!$this->Session->read('clienturl')) { 
        // the first action is the value you want to save e.g. client1
        $this->Session->write('clienturl',$this->action); // save to session
    }
    // set the class property
    $this->clienturl = $this->Session->read('clienturl');
    // for constants: define('CLIENT_URL',$this->Session->read('clienturl'));        
}

这个过程基本上是说“如果会话变量不存在,则从当前操作创建它.然后,将该变量写入类属性/常量”.

正如您目前所做的那样,您需要在每个控制器中调用AppController的(父)beforeFilter.用户控制器示例:

public function beforeFilter() {
    parent::beforeFilter();
    // you can now access:
    // class property: $this->clienturl
    // for constant: CLIENT_URL
    $this->Auth->allow('clientLogin','add');
}

现在在clientLogin中:

public function clientLogin($id = null) {
    $this->autoRender = false;
    $this->RequestHandler->setContent('json','application/json');
    ob_end_clean();

    echo json_encode($this->clienturl);
    // echo json_encode(CLIENT_URL); // if using a constant
}

您可以从应用程序的任何位置访问此变量/常量,但是您需要在当前控制器的beforeFilter中调用AppController的beforeFilter,因为当前控制器的beforeFilter不会扩展AppController的功能,除非您调用AppController,否则它将覆盖它.在控制器的beforeFilter期间运行.

但是,在从操作中保存会话变量的第一页印象之后,您可以从应用程序中的任何位置访问会话变量,而无需调用AppController的beforeFilter,因为会话变量是在第一次展示时创建的(在此处确实调用了它).

echo $this->Session->read('clienturl');

希望这是有道理和有帮助的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读