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


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-11-2008, 02:21 PM
Junior Member
 
Join Date: Jul 2007
Posts: 7
Default Doing the same things for all controllers

Hi,

For every view I use, I wish to set some vars (e.g. the base URL). Is there a way I could do this, without modifying the Zend_Controller_Action class? Yes, you could use the init() method of the Zend_Controller_Action class, but then you would have to add this code to every controller in your web app...

Does anybody know a workaround for this problem?

Thanks in advance!

Kind regards,
Sweepee
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-11-2008, 02:46 PM
cime's Avatar
Junior Member
 
Join Date: Jan 2008
Location: slovenia
Posts: 1
Default

Extend the Zend_Controller_Action controller, and then use that extended controller for your real controllers instead of Zend_Controller_Action.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-11-2008, 04:47 PM
Junior Member
 
Join Date: Jul 2007
Posts: 7
Default

Fantastic idea!

Damn, your answer makes my question so stupid...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-11-2008, 04:51 PM
Senior Member
 
Join Date: Jan 2008
Location: chicago
Posts: 100
Default

Or you could just use the BaseUrl View_Helper



Quote:
Originally Posted by Sweepee View Post
Fantastic idea!

Damn, your answer makes my question so stupid...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-11-2008, 08:35 PM
Junior Member
 
Join Date: Jul 2007
Posts: 7
Default

Quote:
Originally Posted by notrub225 View Post
Or you could just use the BaseUrl View_Helper
Maybe that's an even beter idea, since I'm using multi-lingual routes.

I still have to learn a lot about the Zend Framework.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-20-2008, 05:53 PM
Junior Member
 
Join Date: Mar 2008
Posts: 1
Default

Quote:
Originally Posted by cime View Post
Extend the Zend_Controller_Action controller, and then use that extended controller for your real controllers instead of Zend_Controller_Action.
The problem I have with this method - or creating an Action Helper which in many cases could achieve the same results - is that if you _forward() or use the ActionStack Helper, the same code is processed over again for each controller action that is dispatched. Does anyone have a good suggestion for initialization code that should only be run once for all dispatched controllers?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-10-2008, 08:40 AM
nix nix is offline
Junior Member
 
Join Date: Apr 2008
Posts: 1
Default

Quote:
Originally Posted by dowker View Post
The problem I have with this method - or creating an Action Helper which in many cases could achieve the same results - is that if you _forward() or use the ActionStack Helper, the same code is processed over again for each controller action that is dispatched. Does anyone have a good suggestion for initialization code that should only be run once for all dispatched controllers?
Did anyone come up with anything?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-11-2008, 04:55 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

The init() method of any controller is only run once per controller, regardless of how many actions are run in that controller per request. However if you extend Zend_Controller_Action and put you code in init() then you'll have to call parent::init() on every controller that uses your new controller class.

What you could do is set a flag in session or just the registry that indicates this logic has already run, then check to see if its set before running the logic. This would make a plugin more appropriate.

Code:
$runOnce = Zend_Registry::get('runOnce');
if (!is_null($runOnce)){
    //do something cool, but only once!
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-14-2008, 11:08 AM
Junior Member
 
Join Date: May 2007
Posts: 15
Default

You could also write a controller plugin and register it in your bootstrap.
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 12:39 PM.