Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakePHP: access $this->request->data through view files (information passed by controller)

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?

like image 366
VSB Avatar asked Jan 10 '23 04:01

VSB


1 Answers

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).

like image 164
Alex P Avatar answered Jan 30 '23 09:01

Alex P