Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: How to access $this->request inside a model

I'm on CakePHP v2.4.

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

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

How do I do this?

like image 543
emersonthis Avatar asked Jan 15 '14 14:01

emersonthis


People also ask

How can I get query string in CakePHP?

In CakePHP 2.0 this appears to have changed. According to the documentation you can access $this->request->query or $this->request['url'] .

How can I get post data in CakePHP?

You can retrieve post data as Array. $post_data= $this->request->data; You can retrieve post data for particular key.

What is request object and response object?

The request and response objects provide an abstraction around HTTP requests and responses. The request object in CakePHP allows you to introspect an incoming request, while the response object allows you to effortlessly create HTTP responses from your controllers.


1 Answers

You can access it via the global Router class.

public function afterSave( $created, $options=array() ) {
    $this->log( Router::getRequest() );
}
like image 146
Reactgular Avatar answered Oct 10 '22 20:10

Reactgular