CakePHP: how to access $ this-> request inside model

I am on CakePHP v2.4.

In the afterSave() model callback, I want to do something conditionally, depending on the conditions of the request. However, $this->request is not defined here:

 public function afterSave( $created, $options=array() ) { $this->log( $this->request ); //NOTHING HERE } 

How to do it?

+8
php cakephp
source share
2 answers

You can access it through the global class of the router.

 public function afterSave( $created, $options=array() ) { $this->log( Router::getRequest() ); } 
+22
source share

Instead of calling a single tone (Router :: getRequest ()) in the afterSave () method and entering the tight connection suggested by Matthew, why can't you pass in any parameter you need along with the data of the record you are trying save?

Then it will be present in $ this-> data in afterSave (). Plus, this method is easy to verify. Enjoy testing Router :: getRequest () in this method.

If you need the whole request, you can add the Model :: $ request property in the AppModel and the setRequest () method, as well as in your beforeFilter () controllers you can do $ this → {$ this-> modelClass} → SetRequest ($ this- > request);

+7
source share

All Articles