脚本宝典收集整理的这篇文章主要介绍了cakephp configure :: read value is lost’尝试过常量,同样的行为,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
所以在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');
希望这是有道理和有帮助的.
以上是脚本宝典为你收集整理的cakephp configure :: read value is lost’尝试过常量,同样的行为全部内容,希望文章能够帮你解决cakephp configure :: read value is lost’尝试过常量,同样的行为所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。