You basically want to take the action from the first request, and store it in a constant. You can do that, sure - but constants only last during the current request, so you'll need to use a session to store the value for the next request:
You can share class properties from the AppController to all of your other controllers, because they all extend the AppController, or you can use a constant like you are currently. I'll use a class property for this example.
You need to set the property up in your AppController, and use the Session component:
var $components = array('Session');
public $clienturl = null;
Then your 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'));
}
This process is basically saying "if the session variable doesn't exist, create it from the current action. Then, write that variable to a class property/constant".
And as you are currently doing, you'll need to invoke the AppController's (parent) beforeFilter
in each controller. Users controller example:
public function beforeFilter() {
parent::beforeFilter();
// you can now access:
// class property: $this->clienturl
// for constant: CLIENT_URL
$this->Auth->allow('clientLogin', 'add');
}
And now in 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
}
You can access this variable/constant from anywhere in your app, but you need to have invoked the AppController's beforeFilter
in the current controller's beforeFilter
, because the current controller's beforeFilter
doesn't extend the AppController's function, it will overwrite it unless you invoke the AppController's function during your controller's beforeFilter
.
However, after the first page impression where your session variable is being saved from the action, you can access the session variable from anywhere in the app without having to invoke the AppController's beforeFilter
, because the session variable was created on the first impression (where you did invoke it).
echo $this->Session->read('clienturl');
Hope this makes sense and helps.