0

I had started putting my form element view scripts under '/application/views/scripts/form/' and was able to reference them by 'form/scriptname.phtml', but now I need to make a 'form' controller and I realize this was a short-sighted solution. The examples I've seen use something like '/path/to/your/view/scripts/' which doesn't help me with what's a logical/recommended place to put them.

Thanks!

Sonny
  • 8,204
  • 7
  • 63
  • 134
  • Do you really have to call your controller 'form'? What if you end up with two form controllers? – AJ. Feb 04 '10 at 19:46
  • Controller names have to be unique, so I don't follow your question. The question is more about the best place to put the view scripts though. Regardless of the controller names, I don't think I have placed them optimally. – Sonny Feb 04 '10 at 20:28

1 Answers1

2

I use non-standard file structure and use modules for my applications:

/application
  /default
    /controllers
      /IndexController
      /ErrorController
    /views
      /scripts
        /index
          /index.phtml
        /error
          /error.phtml
/configs
  /config.ini
/library
  /Zend
/views
  /layouts
    /default.phtml
  /scripts
    /form
      /_text.phtml

To do it this way, you have to add the module directory in your config for Zend_Application:

[production]

phpsettings.display_startup_errors = 0
phpsettings.display_errors = 0
resources.layout.layout = "default"
resources.layout.layoutpath = "c:\xampp\files\views\layouts"
resources.frontcontroller.moduledirectory = "c:\xampp\files\application"

[development : production]

phpsettings.display_startup_errors = 1
phpsettings.display_errors = 1

View script paths are loaded in LIFO order. Assuming you have not added any other script paths, you can add your script paths in the action controller init() method this way:

<?php

class IndexController extends Zend_Controller_Action {

  public function init() {

    $appScriptPath = 'c:\xampp\files\views\scripts';
    $modScriptPath = array_shift($this->view->getScriptPaths());

    $this->view->setScriptPath(NULL);

    $this->view->addScriptPath($appScriptPath);
    $this->view->addScriptPath($modScriptPath);

  }

  public function indexAction() {

    $form = new Zend_Form();

    $form->addElement(new Zend_Form_Element_Text('text'));
    $form->text->setLabel('Text');
    $options = array('viewScript' => 'form/_text.phtml');
    $decorators = array(array('ViewScript', $options));
    $form->text->setDecorators($decorators);

    $this->view->form = $form;

  }

}

The system will look in the controller views scripts for your viewScript first, and then if it does not find it, it will look in /application/views/scripts.

  • Why are you using a leading underscore in your view script file name? – Sonny Feb 05 '10 at 14:20
  • 1
    Because it's a ZF naming convention for partial scripts... it's not particularly important but I like to follow naming conventions as much as I can. – Robin M. Canaday Feb 05 '10 at 18:51