I want to implement shared "add" actions in the AppController. For this, I need to access the appropriate model of the derivated controller.
How do I do this?
To get the current, controller: $this->params['controller']
beforeFilter() executes functions that you NEED to be executed before any other action. Controller::beforeFilter() This function is executed before every action in the controller. It's a handy place to check for an active session or inspect user permissions. http://api.cakephp.org/2.3/class-Controller.html#_ ...
Advertisements. The controller as the name indicates controls the application. It acts like a bridge between models and views. Controllers handle request data, makes sure that correct models are called and right response or view is rendered.
You can use: $this->redirect(array("controller" => "myController", "action" => "myAction", "param1" => "val1", "param2" => "val2", $data_can_be_passed_here), $status, $exit); Hope it helps!
The primary model class of a controller is stored in $this->modelClass
, so you could do something like this:
class AppController extends Controller {
function _add($data) {
$this->{$this->modelClass}->save($data);
}
}
class PostController extends AppController {
function someFunction() {
$this->_add($data); // saves to Post model
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With