|
|||
|
Hi there!
I've been working with PHP-OO and Java for the last two years and some days ago I decided to work with Zend in a new project. The problem I've appears when I want to access to a variable in a controller but from another controller. I've tried implementing getters but this doesn't work, I think due to the Zend (MVC) architecture ... Could somebody teach me in the right way please? There is either a specific form to use getters/setters or I should use another Zend method? Thanks! |
|
|||
|
A simple example could be the next one:
You have some information in a DB that you want to read from a controller. Then you generate some code with this and give it to the view. Here is no problem except when you have 2 other controllers that need the same generated code (just to avoid the code duplication, ok?). So, what if I want to do the same I could do with Java or PHP5 ("ZFless")? Thanks in advance! |
|
|||
|
Im also relatively new to the MVC architecture since I have developed all my applications as a single project I have never needed to.
However, isn't the whole idea behind MVC that you only have a single controller. (Please do correct me if I am wrong, I would hate to misdirect people and would like to be corrected). You should never have more then one controller. The controller should handle the users request to decide what to do. It handles the initial logic. The way I think of it is just to figure out what page the user wants it directs the user along the right path. Once you know what page you want, you can use the model to grab any information from the database and then the controller can handle the logic again and pass on the load to the view. You shouldn't do reading in a database from controllers if you are going to output it. You can do logic reading I GUESS?? (correct me) ie if you are checking which site to load (if your database contains 1000s of sites (like bebo??)). Let me know if this is the correct approach. |
|
|||
|
Thanks for your contribution Kalyse.
The idea of having just one controller is good when you have little site (or even huge if you feel comfortable with it) but I think the "good way" goes through having different controllers (i.e.: AdminController, ContentController, ErrorController, ...). So what you said is ok but does not answer my question. Thanks. Somebody else with any idea??? |
|
|||
|
i think that Kalyse is misunderstanding some things. There should be one, but frontcontroller :P
Next, if you want to do the same in several controllers, try to extend model (e. include getMenu() method which return entire menu in form of array, instead of doing this logic in controller). |
|
|||
|
Quote:
You should wholly disregard it as total bs. ![]() |
|
|||
|
I think what you want to do is either:
User Zend_Registry - Or you could use $this->getRequest->setParam('myparameter') = 'value'; You could then access it in your other conroller using $this->getRequest->getParam('myparamterer') //ignore the type. |
|
|||
|
"You have some information in a DB that you want to read from a controller. Then you generate some code with this and give it to the view. Here is no problem except when you have 2 other controllers that need the same generated code (just to avoid the code duplication, ok?)."
Based on the above example I would say that you need to create an action helper. This helper could then be reused in your other controllers. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|