I'm beginning with Zend Framework and I would like to understand Bootstrap file. I've learned all _init
methods are executed by default but it seems confusing to me. Anyway that is not what I would like to ask.
A came around the $this->bootstrap('layout');
action and I'm not sure if I understand this. Is this the resource.layout variable in application.ini file? I would like to really understand the bootstrap process in deep.
I'm asking you for step by step explanation.
Thanks in advance!
So this is my bootstrap file:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload()
{
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH
));
return $moduleLoader;
}
function _initViewHelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$view->headTitle()->setSeparator(' - ');
$view->headTitle('Zend Framework Tutorial');
}
}
The line from application.ini
resources.layout[] =
is equivalent to:
_initLayout() {}
in Bootstrap.php
Both of them are initializing new object, and this object is set as a bootstrap
param, which is a container for some application resources (values returned by _init
methods).
There are executed one by one, so to ensure one resource is initialized before the the current one, you force the order, using:
_initSomeResource() {
$this->bootstrap('otherResource');
// ..
return $someValue;
}
_initOtherResource() {
// ..
}
So the order of instantiating of the resources is:
otherResource
someResource
Now, you may also use:
$bootstrap->getParam('someResource'); // returns $someValue
Note, that you may encounter Circular Dependency
error, when you try to execute each other before each one.
You may use as many _init
methods you need, but to make them reusable, you may separate them to their own class, implementing Zend_Application_Resource_Abstract
class.
There are some out of the box application resources, which you may find in Zend/Application/Resource
directory. These are the resources, you are refering from application.ini
, i.e.:
resources.view.encoding = "utf-8" ; in application.ini
Zend/Application/Resource/View.php (path to the resource class)
Zend_Application_Resource_View::setEncoding('UTF-8'); // equivalent pseudocode
Hope it's more clear now.
refer to this documentation for available options.
Zend_Application will automatically bootstrap anything in the application.ini which begins with resources.
Note that if you do not put something in your ini file, it will not be loaded. E.g. by default no layout is loaded. If you include either one, or both, of the following the layout will be enabled for the application:
resources.layout.layoutPath = APPLICATION_PATH "/layouts"
resources.layout.layout = default
Most important to realise is that it will load the defaults where you have omitted values, let me explain: By default the bootstrap won't have a view resource available, because none is set in the ini. But if you put this in the ini:
resources.view[] =
then you can call:
$this->boostrap('view');
$view = $this->boostrap()->getResource('view');
Then you can do something with your app's view from the bootstrap, e.g.
$view->doctype("HTML5");
Also, your _initAutoload is not necessary anymore, and can be replaced with
appnamespace = ""
in the .ini. I concur that the bootstrapping and ini options are very poorly documented.
////////////////////////////////////////////////////////////////////////////////
else for hands-on learning:
protected function _initIniDump()
{
$ini = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'development');
Zend_Debug::dump($ini->toArray(), 'APPLICATION.INI');die();
}
In that dump you will see that Zend will iterate over the resources array, anything that it recognizes, it loads to best of it's knowledge
:)
Hope that helps.
I have found out that: calling $this->boostrap('resource'); will not work if 'resource' is not in the application/configs/application.ini file. So my answer to your question would be 'yes', you have to define the layout resource in the application.ini file with the following : resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts" (or another dir.)
Unfortunately the zend Framework documentation is really bad in my opinion. Especially on this topic.
My opinion is that you should wait for a version with a better documentation if you want to get deep into the details and workings of the Zend Framework (or you can check the source code of the Zend Library Classes if you have the time).