0

I have a project, it has a structure like this:

http://img526.imageshack.us/img526/2333/92348689.png

I want to make a variable like the following

$templatePath = $this->baseUrl('/application/templates/')` 

and it can be used in many views, in many modules. I think I can do it by declaring the variable in Bootstrap.php (application) but I don't know how to do that.

Siguza
  • 21,155
  • 6
  • 52
  • 89
Killer Whale
  • 87
  • 2
  • 12

3 Answers3

2

Usually, I just place myself such variables into application bootstrap file. Here's an example:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected function _initViewVariables() {
        $this->bootstrap('View');

        $this->view->fooValue = 'foo';
        $this->view->barValue = 'bar';

        $config = Zend_Registry::get('config');

        if( $config->recaptcha->enabled ) {
            $this->view->captcha = true;
            $this->view->recaptchaPublicKey = $config->recaptcha->publicKey;
        }
        else {
            $this->view->captcha = false;
        }

    }

}

I hope it helps!

ADi3ek
  • 645
  • 6
  • 18
1

Base Url is available after routing has been completed (routeShutdown hook) so accessing it in Bootstrap will not work .

So create a controller plugin inside preDispatch() do

public function preDispatch($req) {

$view = new Zend_View();
$view->placeholder('burl')->set(Zend_Controller_Front::getInstance()->getBaseUrl());

}

To access it inside view do like index.phtml

 echo $this->placeholder('burl'); 
Mr Coder
  • 8,169
  • 5
  • 45
  • 74
  • Hi, it works. But I want it can work in any controller, it means that it can work in any view (I have three modules). I don't want type or copy many times this method in other controller. What should I do? – Killer Whale Jun 08 '13 at 07:58
  • @StuartRedman you should then use define('BASE_URL',Zend_Controller_Front::getInstance()->getBaseUrl()); – Mr Coder Jun 08 '13 at 13:44
  • I'm sorry but where I can put define(...)? And Can I pass a string into getBaseUrl()? Ex: Zend_Controller_Front::getInstance()->getBaseUrl('abc')); – Killer Whale Jun 08 '13 at 18:08
0

You could use the Zend_Registry.

In your bootstrap or wherever in your site just State

 Zend_Registry::set("TagLine", "Have a Nice Day");

To use in a view just

 <?= Zend_Registry::get("TagLine"); ?>

for extra credit you could also make a view helper for this (there is one with ZF2)

class My_View_Helper_Registry extends Zend_View_Helper_Abstract
{
    public function registry($key)
    {
        return Zend_Registry::get($key);
    }
}

In your bootstrap you will add a method like:

protected function _initSiteRegistry(){
    Zend_Registry::set("Site_Name", "My Cool Site";
    Zend_Registry::set("Site_TagLine", "Have a nice day";
}

Also if you are using the view helper approach you will also want to register the helper path.. you can do this in _initView in the bootstrap.

    protected function _initView(){
            $view = new Zend_View();
            $view->doctype("HTML5");
            $view->setEncoding("UTF-8");
            $view->addScriptPath(APPLICATION_PATH."/local/views");
            // set this as the viewRenderer's view. 
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $viewRenderer->setView($view);
            $view->addHelperPath("My/View/Helper/", "My_View_Helper_");
            return $view;
    }
Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • How I do it in Bootstrap file? in public function _init(){} or something function ... – Killer Whale Jun 07 '13 at 16:23
  • You could just create a new protected method in the bootstrap called something like `_initSiteVariables` or something. I will update the answer to show an example – Orangepill Jun 07 '13 at 16:26
  • I got an error if I try use $this->baseUrl in _initSiteRegistry(), have no problem with string value => Zend_Registry::set("templatePath", $this->baseUrl("/application/templates/")); – Killer Whale Jun 07 '13 at 16:54
  • baseUrl is a viewHelper you have to be on a view to use it. You can get a servicable view by saying `$this->bootstrap('view'); $view = $this->getResource('view'); $view->baseUrl("/application/templates/");` – Orangepill Jun 07 '13 at 17:00
  • Thanks but I still got an error Uncaught exception 'Zend_Application_Bootstrap_Exception' with message 'Resource matching "view" not found' in C:\xampp\htdocs\zendfirst\library\Zend\Application\Bootstrap\BootstrapAbstract.php – Killer Whale Jun 07 '13 at 17:19
  • protected function _initSiteRegistry() { // baseUrl is a viewHelper $this->bootstrap("view"); $view = $this->getResource("view"); Zend_Registry::set("templatePath", $view->baseUrl("/application/templates/")); } – Killer Whale Jun 07 '13 at 17:20