CakePHP Quick Tips
5 September, 2006 Show comments
Quite often the question is asked "How can I access XYZ in my controller/view/model etc?". IMO this question rarely needs to be asked ;).
Using the following simple approach should empower you to answer most questions:
- Set DEBUG to 2 in your /app/config/core.php file so you can see the executed SQL whilst you are developing, you can set it to 3 to see a dump of the current controller at the foot of the page (might make your page loads a bit cumbersome - especially if you have ajax requests).
- Use the Search facility, pay particular attention to the API.
- Don't be afraid to look at the cake source code; you may be able to quickly see the answer to your question. DON'T make changes to the code (except perhaps temporary debug code) or you may find that upgrading becomes a 2hour task instead of a 2minute one.
- If in doubt stick something like this in your code:
pr ($this); die;
- Don't forget that CakePHP is still PHP :); If you know some data has been set somewhere but you don't know where it is - use what PHP provides such as Get defined vars and search the output on your screen.
If you still don't know where something is after that - think if what you are trying to do is logical, and if you think that it is, it might be worth asking.
One caveat - don't abuse the info that you find. For example: if you see that in your view you can access info that you need in your controller/model/component by navigating from the view to the controller (and on to the model/component if appropriate) - do the right thing and set a variable in your controller to pass it to the view (Info should in principle be sent to a view, it shouldn't need to actively request data*). If you choose not to heed this advice, expect some playful remarks when asking for help from the community if/when Cake removes access to the Controller from the View ;).
* RequestAction can be used in a view to get info from other controllers if required, but it's usually not the best solution.