In an effort to keep my code DRY I would like to be able to define “cross controller” variables.
Classic example is that I would like access to some config items that are loaded in my bootstap.
What is the best practise method of achieving this?
Tim
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can always use the Di container.
Once you register a component in the Di it is available in the controller by the magic method. For instance:
and in your controller it is as simple as:
If you create a base controller class, you can pass those objects in the view if needed like so:
Finally the Di container can act also as a registry, where you store items you might want to use in your application.
For an example of bootstrapping and accessing objects in controllers, have a look at the phalcon/website repository. It implements bootstrapping and base controller patterns among other things.