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


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-29-2008, 05:43 PM
Junior Member
 
Join Date: Apr 2008
Posts: 8
Default custom view helpers

Hi
I made my custom view helper andto use them I have to add my path to the view object:


PHP Code:
$view->addHelperPath("../application/default/views/helpers"); 

But, the problem is that I have to do this in every action where I want to use my helper, and I can't find a way to set this path once, say in a bootstrap....

Is there any easy and fun ways of doing this? and not having to add the path in every action..

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-29-2008, 10:10 PM
Junior Member
 
Join Date: Oct 2007
Posts: 21
Default

If you're using the ViewRenderer it couldn't be simpler:

PHP Code:
$viewRenderer Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->initView();

//your line
$viewRenderer->view->addHelperPath("../application/default/views/helpers"); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-30-2008, 01:59 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

Davidoff's solution is indeed simple, however it appears as though you're using a conventional modular file structure. If so, then once you have helpers in modules other than default Davidoff's solution will stop working for you.

You could set the helper path per request via an Action Helper and set it based on the requested module as so:
PHP Code:
<?php

require_once('Zend/Controller/Plugin/Abstract.php');

class 
MyApp_Controller_Plugin_ModularHelperPath extends Zend_Controller_Plugin_Abstract
{
    public function 
preDispatch(Zend_Controller_Request_Abstract $request)
    {
        
$moduleName $request->getModuleName();
        
$rootDir Zend_Registry::get('rootDir');
        
set_include_path(get_include_path() .
                         
PATH_SEPARATOR $rootDir '/application/' $moduleName '/views/helpers/');
    }
}
$rootDir = Zend_Registry::get('rootDir') assumes you have set the rootDir in your bootstrap, otherwise you will need to adjust the script to set the paths appropriately.
__________________
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-30-2008, 08:02 PM
Junior Member
 
Join Date: Apr 2008
Posts: 8
Default

Quote:
Originally Posted by Elemental View Post
Davidoff's solution is indeed simple, however it appears as though you're using a conventional modular file structure. If so, then once you have helpers in modules other than default Davidoff's solution will stop working for you.

You could set the helper path per request via an Action Helper and set it based on the requested module as so:
PHP Code:
<?php

require_once('Zend/Controller/Plugin/Abstract.php');

class 
MyApp_Controller_Plugin_ModularHelperPath extends Zend_Controller_Plugin_Abstract
{
    public function 
preDispatch(Zend_Controller_Request_Abstract $request)
    {
        
$moduleName $request->getModuleName();
        
$rootDir Zend_Registry::get('rootDir');
        
set_include_path(get_include_path() .
                         
PATH_SEPARATOR $rootDir '/application/' $moduleName '/views/helpers/');
    }
}
$rootDir = Zend_Registry::get('rootDir') assumes you have set the rootDir in your bootstrap, otherwise you will need to adjust the script to set the paths appropriately.
Thanks for the reply guys.
I will try the Davidoffs solution, I was hoping was something that would work by putting it in the bootstrap file once, seems like this should work.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-01-2008, 08:21 AM
Junior Member
 
Join Date: Oct 2007
Posts: 21
Default

Yep - I haven't worked with modules but I think Elemental has it bang on.

I guess you could access the request in the bootstrap and make a decision there, but you might as well use a Controller Plugin.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-01-2008, 12:21 PM
Junior Member
 
Join Date: Apr 2008
Posts: 8
Default

Hi
Davidoff's solution didn't work,
and for the plugin I have a question: why do you include the path instead of using addHelperPath() ?
I tried including the path in the bootstrap file, but it didn't work.
Also the path is: ../application/default/views/helpers
And default it's just a folder that Zend Studio creates (don't know why). But this never changes.
I don't know if you understood me well, I'm new to ZF so maybe some things I don't quite understood.
Thanks for the help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-01-2008, 01:17 PM
Junior Member
 
Join Date: Oct 2007
Posts: 21
Default

I took the line from your example.

But you probably need to pass the second parameter with the class name prefix.

Sample code from my live, working app:

PHP Code:
$viewRenderer Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');

$viewRenderer->initView();

$viewRenderer->view->addHelperPath(APPLICATION_DIRECTORY '/views/helpers''My_View_Helper'); 
Example helper class name is class My_View_Helper_ContactSelect for the helper 'contactSelect'

According to the API Docs, Docs For Class Zend_View_Abstract, the second argument defaults to Zend_View_Helper
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-01-2008, 01:39 PM
Junior Member
 
Join Date: Apr 2008
Posts: 8
Default

Thanks a lot
It worked.
I forgot about the prefix, lol

Many thanks again Davidoff
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 10:06 AM.