|
|||
|
I have come across a need to do either of the following in a view script.
1 - Call a function in the Controller that called the view. 2 - Create a function inside the view file. Are either of these good practise? Is 1 even possible? Do I need to create a helper or something like that instead? It's specific to the view so a helper may be over the top. I guess creating a function in the view script is not ideal but probably is the most practical thing to do? |
|
||||
|
Any additional functionality you need in the view should be done in a view helper.
Ideally you should be performing all business/application logic before you get to the view. If you could give us some more info on why you need to get back to the controller perhaps we can help you clean up your design.
__________________
Zend Framework Resources: Zend Webinars | Reference Manual | API Docs | Books | FreeNode: #zftalk Getting Started Tutorials: Getting started with ZF | Getting started with Zend Auth |
|
|||
|
Quote:
Basically I'm creating an ACL admin screen that shows a matrix in a table. The rows are controller / action names, and the columns are group names. Each cell contains a checkbox indicating whether that group can access the action/controller on that row. When initially rendering the page I need to have some boxes checked depending on the current access. For example user 'Admin' will have all of its boxes checked when the page loads. I have assigned 'groups' and 'resources' arrays to the view and I'm looping through them to build the table. When creating the checkboxes I want to do something like: <?php ($this->isAllowed($group, $controller, $action)) ? 'checked="checked" : '' ?> So I can do this with a view helper, which may be a good idea as later on I may only want to show an 'edit' or 'delete' button on my pages based on the same isAllowed function. At the moment I'm sending an array to the view, but it's very messy. The above becomes: <?php ($this->aclMatrix[$group . '_' . $controller . '_' . $action] == 1) ? 'checked="checked" : '' ?> I think view helper is the way to go. I have only just realised that it would be useful outside of this page, which was the deciding factor in using one. For interests sake though, I'd like feedback still. There'd have to be some time where it makes sense to have a function that only one view needs access to. |
|
||||
|
I have a view helper that contain several misc functions. I use it to store these types of one off methods that my view needs but doesn't really require a full seperate object. I really only do it this way for lack of a better idea.
Generally speaking tho, if your view is in need of a single function that won't be used anywhere else, you should examine your design. There is most likely a better way to handle it. My "understanding" or "methodology" for using the ZF MVC is like this. The controller should take care of getting all the data the view needs from the model and having it formatted correctly (some would argue that formatting belongs in the view, what I mean here is does the data need to be in object, array, JSON, etc. formats). The model handles all the business logic for your data to ensure it's sanitary and accurate. The view simply displays the data. There are some nifty view scope logic tricks that can be applied and they should become view helpers. The view should not act upon the data, except to make it look it right and decide how to display it. That's my 2 cents anyway...
__________________
Zend Framework Resources: Zend Webinars | Reference Manual | API Docs | Books | FreeNode: #zftalk Getting Started Tutorials: Getting started with ZF | Getting started with Zend Auth |
|
|||
|
Quote:
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|