Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-24-2008, 06:18 AM
Junior Member
 
Join Date: May 2007
Posts: 15
Default View and Controller interfaction

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-24-2008, 09:49 AM
Member
 
Join Date: Aug 2007
Location: Sweden
Posts: 52
Send a message via MSN to Leif.Högberg
Default

While functions in the view are entirely possible, a view helper is the way to go.

Why do you need to call a function in the controller? Can the controller not process the data before it's passed to the view?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 02:15 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 122
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-24-2008, 11:31 PM
Junior Member
 
Join Date: May 2007
Posts: 15
Default

Quote:
Originally Posted by Elemental View Post
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.
I most definitely agree with your comments.

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-25-2008, 03:02 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 122
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-26-2008, 05:59 AM
Junior Member
 
Join Date: May 2007
Posts: 15
Default

Quote:
Originally Posted by Elemental View Post
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...
Having a view helper like that sounds like a good idea. Also I definitely agree with you. I guess really I need to send a custom object to the view, probably a model object that handles ACL rules, and then I can call that objects 'isAllowed' function. I've given a lot of thought to this problem over the last few days and I agree that I should either be using a view helper, or using an object that has functions useful to me. Even though this may only be used by one view file, it is still not the job of the view file, so it needs to be managed externally to maintain good design.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 09:29 PM.