Cakephp configure :: read value lost 'try constant, same behavior

My application runs on the controlleR page; The first thing that applications need is to set the configuration value - from url - which I want to use for the entire application, for example localhost / laborbase / client1, I need to save "client1" as a constant.

So, in AppController, beforefilter, I assign a constant (checked using Configure ::, but with the same problem: the value is lost for other controllers.

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');
}

and display the value for testing when the page action is called (Pages / Display

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

From UserController I cannot access the value: constant comes undefined

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);
}

And not available for other controllers. Trial configuration :: and the same.

.

?

+4
3

. , , , :

AppController , AppController, , . .

AppController :

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
}

/ , AppController beforeFilter beforeFilter, beforeFilter AppController, , AppController beforeFilter.

, , , AppController beforeFilter, ( ).

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

, .

+3

PagesController, PagesController. , , ( beforeFilter, PagesController) appController.

+1

. , .

CakePHP SessionComponent .

, , . , .

Configure , , . , clientLogin() , sizeOf ($ clienturl) == 3 , CLIENT_URL - .

+1

Source: https://habr.com/ru/post/1526078/


All Articles