How to access $this->request->data
passed in by controller inside Views? e.g. variable defined by $this->set('sample')
inside controller can be accessed by $sample
inside view and .ctp files. How can I access values stored inside $this->request->data
through view files?
The CakePHP book says that $this->request
is available within Controllers, Views, and Helpers. So you can access it using $this->request->data
in your view. If you wanted to give it a shorter name, you could set it to something in your controller:
$this->set('requestData', $this->request->data);
If tyour view only needs a couple of variables, it may be clearer to unpack the request data in your controller and pass them in directly. This would also be better separation of concerns; if you refactor your application later, you won't have to update the view as long as you pass in those parameters too:
$this->set('name', $this->request->data('name'));
$this->set('age', $this->request->data('age'));
(Note that I'm using the CakePHP data()
method to access these properties; you don't have to treat it as an array).
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