Question

I have a login button in the header of the website. This header's html is programmed into Zend framework views/layouts/home.phtml.

I have a hidden form in this layout that is triggered by jQuery thickbox inline content display integration. Reason, I dont want to make a ajax call to just fetch a small login form.

I create the form using Zend_Form and the problem is that I have to do it in all the controllers after checking if the user is logged in or not. I want to place this form generation in one single place, say in bootstrap and then have a logic in bootstrap to say that if user is logged in dont generate the form.

I don't know if bootstrap is the right place to do so or should I do it in some other place.

So, where should I instantiate the form so that its available everywhere if user is not logged in.

Was it helpful?

Solution 3

I did it in a different way, extendingZend_Controller_Plugin_Abstract to implement a plugin and register it with front controller.

public function routeStartup(Zend_Controller_Request_Abstract $request) { } 

generated the form inside the above mentioned method and by setting the form in $view object.

$view can be retrived using :

$view = Zend_Layout :: getMvcInstance()->getView();

OTHER TIPS

Create your own base controller which extends Zend_Controller_Action then have your controllers extend off of your base controller. I don't know what "jQuery thickbox inline content display integration" is...but you have several sections you can put it in depending when you need your code to run. init(), preDispatch(), postDispatch() etc... Just make sure when you extend off your base controller that you do sthing like:

parent::init() parent::preDispatch() parent::postDispatch() etc... within each section so that the base code runs as well...

Be careful about Pradeep Sharma's solution (the answer he wrote himself and accepted below).

All the code code below is for ZF 1.12, and not ZF 2.0

In the bootstrap, Zend_Layout's MVC instance might not have been created yet. You should use Zend_Layout::startMvc() instead :

$view = Zend_Layout::startMvc()->getView() ;

And tbh I prefer executing this code in the preDispatch() function. New users of ZF might be interested in this :

application/plugins/HeaderForm.php :

class Application_Plugin_HeaderForm extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {    
        $view = Zend_Layout::startMvc()->getView() ;
        $view->headerForm = new Application_Form_HeaderForm() ;
    }
}

Calling new Application_Form_HeaderForm() will autoload by default into application/forms/ folder. You can also create the form directly into the plugin with new Zend_Form(), and addElement() etc. but it won't be reusable.

Of course, you need to register this plugin in your bootstrap!

application/Bootstrap.php :

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initPlugin()
    {
        $front = Zend_Controller_Front::getInstance() ;
        $front->registerPlugin(new Application_Plugin_HeaderForm()) ;
    }
}

Calling new Application_Plugin_HeaderForm() will autoload by default into application/plugins/ folder

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top