How to share data across all view and controllers
Permalink
Hi,
I have found a few resources that deal with custom controllers and models but nothing that quite addresses the scenario I have.
I have a multi-dimensional array of countries that should be support by a custom shopping cart on my site. This array is used to building various parts of the theme interface (such as a dropdown that the user can use to switch their location and therefore pricing), as well as by my shopping cart controller and single pages to apply the correct currency and shipping charges.
My question is: where is the best place for this array to go so that it can be accessed from all across my site, by view files and controllers?
Thanks
I have found a few resources that deal with custom controllers and models but nothing that quite addresses the scenario I have.
I have a multi-dimensional array of countries that should be support by a custom shopping cart on my site. This array is used to building various parts of the theme interface (such as a dropdown that the user can use to switch their location and therefore pricing), as well as by my shopping cart controller and single pages to apply the correct currency and shipping charges.
My question is: where is the best place for this array to go so that it can be accessed from all across my site, by view files and controllers?
Thanks
Many thanks for your reply. I do have a class within application/src. If I create this array in this place, how would I then access it within a theme view file to be able to loop through it? Currently this class is only called manually from the single page that requires it.
I have considered putting it in the DB and creating a model for it, however at the moment I want to keep it more simple.
I have considered putting it in the DB and creating a model for it, however at the moment I want to keep it more simple.
I'm getting a bit out of my depth, so hopefully someone more erudite can chime in.
That said, my understanding is that you should be able to access your controller class from anywhere via its namespace and classname; eg, \My\Namespace\MyControllerClass. If your data is static (which would be a valid option since you presumably don't need multiple copies of it), you can access your data via that class (perhaps via getter methods).
If you don't want to make your data static, you may need to instantiate an object of your class. This raises scary questions about the lifetime of controller objects in concrete5. Careful testing would be required.
A singleton class is an intermediate possibility, since it would avoid duplicate data while also avoiding the 'impurity' of static class data. Again, lifetime issues are a concern, and such a class would presumably need to be separate to your controller because, I assume, c5 may need to instantiate multiple controller objects if there are multiple copies of your block(?) on one page.
That said, my understanding is that you should be able to access your controller class from anywhere via its namespace and classname; eg, \My\Namespace\MyControllerClass. If your data is static (which would be a valid option since you presumably don't need multiple copies of it), you can access your data via that class (perhaps via getter methods).
If you don't want to make your data static, you may need to instantiate an object of your class. This raises scary questions about the lifetime of controller objects in concrete5. Careful testing would be required.
A singleton class is an intermediate possibility, since it would avoid duplicate data while also avoiding the 'impurity' of static class data. Again, lifetime issues are a concern, and such a class would presumably need to be separate to your controller because, I assume, c5 may need to instantiate multiple controller objects if there are multiple copies of your block(?) on one page.
Another thought: I remembered this:http://documentation.concrete5.org/developers/working-with-pages/si... . It alludes to data being held independently to the controller. Alas, details are scant, but such an approach does less violence to the MVC architecture.
Thanks for your replies. The class certainly can be static since it's fundamentally just going to contain a read-only array. I'm going to follow your suggestion to include the data there within my existing basket class. The only issue is that I'm not sure how clean it is to include this class directly within a view file, however your link led me via a series of articles to this:
http://documentation.concrete5.org/developers/working-with-pages/co...
I'm thinking the best thing would be to create a page type controller (the whole site only uses a single page type), and in this call the static class and pass the array data through to the view. This seems to tick all the boxes whilst keeping things clean and 'MVC'.
Would be interested if that's the best approach.
http://documentation.concrete5.org/developers/working-with-pages/co...
I'm thinking the best thing would be to create a page type controller (the whole site only uses a single page type), and in this call the static class and pass the array data through to the view. This seems to tick all the boxes whilst keeping things clean and 'MVC'.
Would be interested if that's the best approach.
A more radical alternative would be to store your content in the c5 database.