Hi,
I trying to migrate my application from using ZF 0.9.3 to ZF 1.0.0rc1 .
I've read the doc about the new helpers and the migration but I can't figure
out what I need to change in my code.
My code his heavily based on the ZF tutorial found on the dev wiki.
Here's what I have
in bootsrap.php
...
require 'Zend/Controller/Front.php';
$frontController = Zend_Controller_Front::getInstance(); // manages
the overall workflow
$frontController->addModuleDirectory($appDir . 'modules');
// Initialize views
//require 'Zend/View.php';
self::$view = new Zend_View($config->view->toArray());
self::$view->strictVars(); // enables tracking/detection of typos
and misspelled variables in views
// do not show sensitive logs to requests from non-local networks
if (self::isLocalRequest()) {
self::$view->showLog = $config->view->showLog;
}
self::$view->sectionName = basename($installDir); // e.g.
"application"
self::$view->viewSuffix = 'phtml'; // file name suffix for view
script
// default location shared by all modules
self::$view->setScriptPath($appDir . 'modules' . $ds . 'default' .
$ds . 'views' . $ds . 'scripts');
Simon_Log::log("scriptPaths=\n " . implode("\n ",
self::$view->getScriptPaths()));
self::$view->timezone = $config->timezone; // default timezone
self::$view->now = $now = date('Y-m-d H:i:s');
/////////////////////////////
// ==> SECTION: mvc <==
// Cause controllers to use private views, but with inheritance (i.e.
defaults in self::$view)
$frontController->setParam('view', clone self::$view); // make
private presentation model for controller
$frontController->setParam('registry', self::$registry); //
alternative to Zend_Registry::getInstance()
return $frontController;
then in application/lib/Controller/Action.php
<?php
/** Zend_Controller_Action */
require_once 'Zend/Controller/Action.php';
abstract class Simon_Controller_Action extends Zend_Controller_Action
{
protected $_redirector = null;
/**
* Initialize the controller
*/
public function init()
{
parent::init(); // just good habit (nothing there currently)
// make the view available to action controllers for holding
presentation model
$this->view = $this->getInvokeArg('view');
$this->view->baseUrl = $this->_request->getBaseUrl();
if (!empty($this->view->viewSuffix)) {
// i.e. do not alter the view's filename suffix, if specified by
the application
$this->viewSuffix = $this->view->viewSuffix;
}
// now give the module a chance to initialize itself and the view
used by the module's controllers
$moduleName = $this->_request->getModuleName();
$class = 'ZFModule_' . ucfirst($moduleName);
if (!class_exists($class)) {
$registry = Zend_Registry::getInstance();
$moduleInit = $registry['appDir'] . 'modules' .
DIRECTORY_SEPARATOR
. $moduleName . DIRECTORY_SEPARATOR . $moduleName
. '.php';
if (is_readable($moduleInit)) {
include_once $moduleInit;
}
}
if (class_exists($class, false) && method_exists($class,
'moduleInit')) {
call_user_func(array($class, 'moduleInit'), $this->view);
}
// initialize the redirector
if ($this->_redirector === null) {
$this->_redirector = $this->_helper->getHelper('Redirector');
}
}
public function initView(array $newViewOptions = array())
{
require_once 'Zend/View/Interface.php';
if (!empty($this->view) && !($this->view instanceof
Zend_View_Interface)) {
throw new Zend_Controller_Exception("'" . (gettype($this->view)
=== 'object'
? get_class($this->view) : $this->view) . _("' does not
implement Zend_View_Interface."));
}
$request = $this->getRequest();
$module = $request->getModuleName();
$dirs = $this->getFrontController()->getControllerDirectory();
if (empty($module) || !isset($dirs[$module])) {
$module = 'default';
}
$baseDir = dirname($dirs[$module]) . '/views';
if (!file_exists($baseDir) || !is_dir($baseDir)) {
throw new Zend_Controller_Exception(_('Missing base view
directory ("%1$s"', $baseDir));
}
// if we don't have a view yet, create one
if (empty($this->view)) {
require_once 'Zend/View.php';
$options = array_merge(array('basePath' => $baseDir),
$newViewOptions);
$this->view = new Zend_View($options);
} else {
// This is our chance to automatically *prepend* paths for the
active module,
// without having side-effects for other modules that might
become active later
// via an internal reroute (see bootstrap.php's reroute() and
preDispatch()).
// Since the current view object "inherited" from the view
object setup in bootstrap.php,
// this view object already has default paths pointing to the
default module's
// script/helper/filter paths. Thus, the views in ZFDemo
modules can use default
// scripts, helpers, and paths shared via the default module.
$this->view->addScriptPath($baseDir . '/scripts')
->addHelperPath($baseDir . '/helpers')
->addFilterPath($baseDir . '/filters');
// We could support $newViewOptions here, but code would not be
DRY with respect
// to Zend_View_Abstract::__construct(), and the view was
already created elsewhere,
// so there was already an opportunity to supply options.
}
return $this->view;
}
/**
* Convenience method: render a view in <module
directory>/views/scripts/<controller name><action name>.<viewSuffix>
*
* @param string $segmentName OPTIONAL render view to named segment
*/
public function renderToSegment($segmentName = null, $script = null)
{
$this->render($script ? $script :
$this->getRequest()->getControllerName()
. ucfirst($this->getRequest()->getActionName()),
$segmentName, true);
}
}
and then finaly in application/modules/ged/DocumentsController.php
<?php
class Ged_DocumentsController extends Simon_Controller_Action
{
// document id requested (if any)
protected $documentId = false;
// documents model (list of documents)
protected $documents;
/**
* The default action is "indexAction", unless explicitly set to
something else.
* Show a list of all documents.
*/
public function indexAction()
{
$this->displayStage3_4();
$this->displayStage5();
}
/**
* STAGE 3: Choose, create, and optionally update models using business
logic.
* STAGE 4: Apply business logic to create a presentation model for the
view.
*/
protected function displayStage3_4()
{
// SimonModel_Document provides static methods that auto-instantiate
and manage the model
$this->view->documents =
SimonModel_Ged_Document::getPresentationModel();
$this->view->user = 'anonymous';
// Controller = 'index', Action = 'index', param name = 'document'
$this->view->documentUrl = 'index/index/document';
}
/**
* STAGE 5: Choose view and submit presentation model to view.
*/
protected function displayStage5()
{
// Hard way:
//$this->_response->appendBody($this->view->render('
indexDocuments.phtml'));
$this->renderToSegment('body'); // initiate STAGE 6 in the view
template
}
/**
* redirect bogus URLs back to the application's "home" page
*/
public function __call($name, $parameters)
{
$this->_redirect('/');
}
}
The problem I get is that once in the view script, the view object has been
reset by the viewRenderer helper and I'm unable to display any data I assign
to the view.
Thanks
--
Martin Carpentier