Programmer's Reference Guide
| Das Projekt erstellen |
Ein Layout erstellen
Wie man festgestellen kann waren die View Skripte im vorhergehenden Kapitel HTML Fragmente- und keine kompletten Seiten. Das ist so gewünscht; wir wollen das unsere Aktionen nur den Inhalt zurückgeben der für die Aktion selbst relevant ist, und nicht die Anwendung als ganzes.
Jetzt müssen wir den erstellten Inhalt zu einer kompletten HTML Seite zusammenfügen. Wir wollen auch ein konsistentes Aussehen und Feeling für die Anwendung haben. Wir wollen ein globales Sitelayout verwenden um beide Arbeiten zu ermöglichen.
Es gibt zwei Design Pattern die Zend Framework verwendet um Layouts zu implementieren: » Two Step View und » Composite View. Two Step View wird normalerweise mit dem » Transform View Pattern assoziiert; die grundsätzliche Idee besteht darin das die View der Anwendung eine Repräsentation erstellt die dann in die Master View für letzte Transformationen injiziert wird. Das Composite View Pattern arbeitet mit einer View die aus ein oder mehreren atomischen Anwendungs Views gemacht ist.
Im Zend Framework kombiniert Zend_Layout die Ideen hinter diesen Pattern. Statt dass jedes Action View Skript Site-weite Artefakte einfügen muss, können Sie sich einfach auf Ihre eigenen Beantwortungen konzentrieren.
Natürlich benötigt man trotzdem Anwendungs-spezifische Informationen im Site-weiten View Skript. Glücklicherweise bietet Zend Framework eine Anzahl von View Platzhaltern die es erlauben solche Informationen von den Action View Skripten zu bekommen.
Um mit Zend_Layout zu beginnen müssen wir als erstes die Bootstrap informieren das Sie die Layout Ressource verwenden soll. Das kann getan werden indem der Befehl zf enable layout verwendet wird:
- % zf enable layout
- Layouts have been enabled, and a default layout created at
- application/layouts/scripts/layout.phtml
- A layout entry has been added to the application config file.
Wie vom Kommando notiert wird application/configs/application.ini aktualisiert und enthält jetzt das folgende im Abschnitt production:
- ; application/configs/application.ini
- ; Zum Abschnitt [production] hinzufügen:
- resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
Die endgültige INI Datei sollte wie folgt aussehen:
- ; application/configs/application.ini
- [production]
- ; PHP Einstellungen die initialisiert werden sollen
- phpSettings.display_startup_errors = 0
- phpSettings.display_errors = 0
- includePaths.library = APPLICATION_PATH "/../library"
- bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
- bootstrap.class = "Bootstrap"
- appnamespace = "Application"
- resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
- resources.frontController.params.displayExceptions = 0
- resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
- [staging : production]
- [testing : production]
- phpSettings.display_startup_errors = 1
- phpSettings.display_errors = 1
- [development : production]
- phpSettings.display_startup_errors = 1
- phpSettings.display_errors = 1
Diese Direktive sagt der Anwendung das Sie nach Layout View Skripten unter application/layouts/scripts nachschauen soll. Wenn man den Verzeichnisbaum betrachtet sieht man das dieses Verzeichnis jetzt, zusammen mit der Datei layout.phtml, erstellt wurde.
Wir wollen auch sicherstellen das wir eine XHTML DocType Deklaration für unsere Anwendung haben. Um das zu aktivieren mussen wir eine Ressource zu unserer Bootstrap hinzufügen.
Der einfachste Weg um eine Bootstrap Ressource hinzuzufügen ist es einfach eine geschützte Methode zu erstellen die mit der Phrase _init beginnt. In diesem Fall wollen wir den Doctype initialisieren, also erstellen wir eine _initDoctype() Methode in unserer Bootstrap Klasse:
- // application/Bootstrap.php
- class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
- {
- protected function _initDoctype()
- {
- }
- }
In dieser Methode müssen wir der View sagen das Sie den richtigen DocType verwenden soll. Aber wo kommt das View Objekt her? Die einfachste Lösung ist die Initialisierung der View Ressource; sobald wir Sie haben können wir das View Objekt aus der Bootstrap holen und verwenden.
Um die View Ressource zu initialisieren ist die folgende Zeile in der Sektion production der Datei application/configs/application.ini hinzuzufügen:
- ; application/configs/application.ini
- ; Zum Abschnitt [production] hinzufügen:
- resources.view[] =
Das sagt uns das die View ohne Optionen initialisiert werden soll ('[]' zeigt das der "view" Schlüssel ein Array ist, und wir Ihm nichts übergeben).
Jetzt da wir die View haben, sehen wir uns die _initDoctype() Methode an. In Ihr stellen wir zuerst sicher das die View Ressource läuft, holen das View Objekt und konfigurieren es anschließend:
- // application/Bootstrap.php
- class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
- {
- protected function _initDoctype()
- {
- $this->bootstrap('view');
- $view = $this->getResource('view');
- $view->doctype('XHTML1_STRICT');
- }
- }
Jetzt da wir Zend_Layout initialisiert und den DocType gesetzt haben erstellen wir unser Site-weites Layout:
- <!-- application/layouts/scripts/layout.phtml -->
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Zend Framework Schnellstart Anwendung</title>
- </head>
- <body>
- <div id="header" style="background-color: #EEEEEE; height: 30px;">
- <div id="header-logo" style="float: left">
- <b>ZF Schnellstart Anwendung</b>
- </div>
- <div id="header-navigation" style="float: right">
- <a href="<?php echo $this->url(
- array('controller'=>'guestbook'),
- 'default',
- true) ?>">Guestbook</a>
- </div>
- </div>
- </body>
- </html>
Wir holen den Inhalt der Anwendung indem der layout() View Helfer verwendet, und auf den "content" Schlüssel zugegriffen wird. Man kann andere Antwort Segmente darstellen wenn man das möchte, aber in den meisten Fällen ist das alles was notwendig ist.
Es ist zu beachten das wir auch den headLink() Platzhalter verwenden. Das ist ein einfacher Weg um das HTML für das <link> Element zu erstellen, sowie um es durch die Anwendung hindurch zu verfolgen. Wenn man zusätzliche CSS Blätter zur Unterstützung einer einzelnen Aktion benötigt, kann das getan werden indem man sicherstellt das Sie in der endgültig dargestellten Seite vorhanden sind.
Hinweis: Checkpoint
Jetzt gehen wir zu "http://localhost" und prüfen die Quelle. Man sieht den XHTML Header, Kopf, Titel und Body Abschnitte.
| Das Projekt erstellen |
Add A Comment
Please do not report issues via comments; use the ZF Issue Tracker.
If you have a JIRA/Crowd account, we suggest you login first before commenting.

Comments
Message: Invalid controller specified (guestbook)
Request Parameters:
array (
'controller' => 'guestbook',
'action' => 'index',
'module' => 'default',
)
there is some problem with controller, but i'm trying to do web app first time, and its to hard to handle this problem .. pls help..
The problem was on the version of the Zend Framework in Zend Server CE. I've downloaded the latest release of ZF and extracted it to C:\Program Files\Zend\ZendServer\share\ZendFramework and it solved the problem
That only happens when you click the "Guestbook" hyperlink at the top right portion of the webpage, right?
That's because the guestbook controller was not created yet, check out the next of this guide:
http://framework.zend.com/manual/en/learning.quickstart.cre
An Error Has Occurred
Action 'enable' is not a valid action.
Thanks
line and just add this to application.ini:
resources.view.doctype = "XHTML1_STRICT"
" An Error Has Occurred
A project profile was not found.
Zend Framework Command Line Console Tool v1.10.2
Details for action "Enable" and provider "Layout"
Layout
zf enable layout"
Please help me?
You mast be inside your project folder and than run this command
I have different problem. After enabling layout I get Internal Server Error. Why?
sudo add-apt-repository ppa:zend-framework/ppa
sudo apt-get update
sudo apt-get install zend-framework
sudo apt-get install zend-framework-bin
sudo apt-get install libzend-framework-php
I'm not able to view the mentioned header, footer and contents. I've not used the command zf enable layout. The Zend welcome page is still shown at that URL.Please guide me the steps to follow to identify the problem.
My application.ini looks like this:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "zendtest"
resources.db.params.password = "zendtest12"
resources.db.params.dbname = "zendtest"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "zendtest"
resources.db.params.password = "zendtest12"
resources.db.params.dbname = "zendtest"
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "zendtest"
resources.db.params.password = "zendtest12"
resources.db.params.dbname = "zendtest"
If there is any query, please let me know.
Thank You,
Neeraj
but isn't working, I got the message: "Action 'enable' is not a valid action.
So I have a look at my zf version with "zf show version" --> 1.9.6 and this is really myterios because i downloaded and installed the version 1.10.3
Can someone advice what I can do that this is working?
Thanks and regards, Wolfgang
I found that I had installed xampp and it came with it's own version of Zend
Once I replaced BOTH THE "ZEND" FOLED AND THE "zf.bat,zf.php" Files it all worked fine.
I also had the same problem.
Whether you use the Zend server in windows?
When you installed Zend server, it added the system path. Try this, at the command prompt type PATH. There are "C:\Program Files\Zend\ZendServer\share\ZendFramework\bin".
You should change the system path to the new version. Or, just replace ZF on ZendServer\share\ZendFramework with new version.
Now, try "zf show version" :D
Any tips?
The path to the Zend framework installation should be registered with the system.
Eg: In Windows a path variable should be set in the Environment Variables in the Advanced tab of System Properties in Control Panel. (eg: Control Panel->System->Advanced->Environment Variables in XP).
I'm using XAMPP. There is a small caveat in there which I'll share with my fellow XAMPP users. XAMPP has already has some logic by which all the folders inside it knows the existence of others. So If you go look inside it you can see there is already a Zend folder in the PEAR directory inside php folder. So all we need to add as a path variable is up to the php folder (eg: C:\xampp\php) and all will be well.
Check by typing "zf show version". It will print out the version of the Zend libraries inside the PEAR folder. (Whenever there is a new version of ZF is available I change the content inside this folder and everything is up to date).
Now for those who can make there virtual hosts work.
A better option is to add the following line in http.conf
include conf/extra/httpd-vhosts.conf
and add all the code given here for httpd.conf inside http-vhosts.conf (This is the preferred way since Apache 2.2)
Also somehow you will not be able to access your http://localhost/ as earlier so add an alias for localhost like localhosts.tld in etc/hosts file also. :-)
edit httpd.conf as shown below:
Listen 10088
Listen 80
<VirtualHost *:10088>
ServerName localhost
DocumentRoot /usr/local/zend/apache2/htdocs
</VirtualHost>
<VirtualHost *:80>
ServerName quickstart.local
DocumentRoot /usr/local/zend/apache2/htdocs/quickstart/public
SetEnv APPLICATION_ENV "development"
<Directory /usr/local/zend/apache2/htdocs/quickstart/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
edit hosts file (etc/hosts) as shown below:
127.0.0.1 localhost
127.0.0.1 quickstart.local
now I have my original host at port 10088 and the quickstart project served from port 80
I am a novice for ZF. I followed your instructions from start to here(layout). just now when I browse my proj: from localhost, the layout/layout.phtml is not working. It was show only the index/index.php only.
I am using XAMPP 1.7.3 ( window)
Please help me. I am waiting your advice.
Thank
I use Windows 7
Xampp 1.7.2
Zend Framework 1.10.5
There is another zf.bat in xampp/php. Delete it and set the enviroment variable to your path/bin
My is c:\zend\bin
All commands are working now without problem...and the solution was very easy.
windows xp
xampp v.1.7.3
zf show version = zend framework version: 1.9.6
virtualhost run
"zf enable layout" = " Action 'enable' is not a valid action."
what is the problem?
thx@all
windows xp
xampp v.1.7.3
zf show version = zend framework version: 1.9.6
virtualhost run
"zf enable layout" = " Action 'enable' is not a valid action."
what is the problem?
thx@all
windows xp
xampp v.1.7.3
zf show version = zend framework version: 1.9.6
virtualhost run
"zf enable layout" = " Action 'enable' is not a valid action."
what is the problem?
thx@all
zf create project quickstart
zf create config
-- copy the files in the created project (application map, xml file etc.) to the bin
zf enable layout
it very useful to me...hope a new guidelines will soon.
zf enable layout, the fix is quite simple...in fact, it's not even a fix, it's just common sense that wasn't so common when I first started...
So lets get the terminal open and create our zf alias using the zf.sh (if you're on this page you should already know how to do this)
In my case:
alias zf=/usr/local/zend/share/ZendFramework/bin/zf.sh
After this you should be able to type in "zf show version" and the version will appear.
After that, I put in "zf enable layout", but got an error (Project profile not found)
Here's what you simply need to do
use a "cd" (change directory) to that project directory (you should see an xml document in it, .zfproject.xml in my case. That's the profile it's looking for.
In my case:
cd /usr/local/zend/apache2/htdocs/quickstart
THEN go ahead with "zf enable layout"
Good luck!
******** ZF ERROR *******
In order to run the zf command, you need to ensure that Zend Framework
is inside your include_path. There are a variety of ways that you can
ensure that this zf command line tool knows where the Zend Framework
library is on your system, but not all of them can be described here.
The easiest way to get the zf command running is to allow is to give it
the include path via an environment variable ZEND_TOOL_INCLUDE_PATH or
ZEND_TOOL_INCLUDE_PATH_PREPEND with the proper include path to use,
then run the command "zf --setup". This command is designed to create
a storage location for your user, as well as create the zf.ini file
that the zf command will consult in order to run properly on your
system.
I added ZEND_TOOL_INCLUDE_PATH to my environment variables (with the value C:\zf\library in my case), restarted the command shell, ran zf --setup, and now it all works.
Hope this helps someone else who is struggling.
In which directory do i have to execute it?
have made all the correction above
I had problems with zf enable layout also. So after I ran it, I created the new directory myself and I created a new empty file called:
application/layouts/scripts/layout.phtml
I followed the rest of the instructions and it is working fine. I could not get the virtual host to work (last lesson), however I just tweaked the httpd.conf file to look like this. Of course, this might bite me later, but so far it is okay. Also, I'm using port 8080 and it works nicely.
....
DocumentRoot "C:/ZendFramework-1.10.8/bin/quickstart/public"
....
<Directory "C:/ZendFramework-1.10.8/bin/quickstart/public">
DirectoryIndex index.php
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
If the /public/css/global.css has any text in it whatsoever, there will not be any attempt made to find a controller named "css".
When I first read the code snippet, I thought that css/global.css was generated automatically (as some kind of default style) by the zf tool at layout creation time. But then, I didn't find a global.css file anywhere, so I thought- hmm, am I suppose to create this file? If so, how come there's no mention of it?
Is is how "Nigel King on: 2010-08-11 09:48:07" wrote:
Add ZEND_TOOL_INCLUDE_PATH to your environment variables with path to the Zend-Library (eg: C:\Zend\library\)
Update your xampp Zend directory with the latest version of Zend
(eg: C:\xampp\php\PEAR\Zend\)
Check the directory to your "User Settings" and look for the folder ".zf".
In my case it doesnt exist and dont try to create it with the Windows Explorer.
It won't work.
Start the "cmd"-shell, go to C:\User settings\your_username\ .
Then type "mkdir .zf"
Next, change to your project-directory where you find the ".zfproject.xml" and type
"zf enable layout"
That's all!
Perhaps you have to enter "zf --setup" before enabling the layout.
i had to manually type in the lines:
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
and create the folders layouts/scripts with the layout.phtml inside
i cant believe im the only one going through this cos ive googled like hell and NOT found the error i mentioned in the previous threads ive posted
to note im using xampp-32bit on windows7 64bit, but i didnt find a 64-bit version on the apachefriends site
it's quite discouraging in the end. im gonna try one last day and shift to CakePHP just like everyone else
but in my case it says layout is not a valid provider??? any help???
You don't have permission to access /quickstart/public/ on this server.
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
to my Bootstrap.php my page output bacame plain balnk page.
where is my error? cant find it
id just follow the steps
The documentation here is wrong. Instead of adding to the application.ini:
resources.view[] =
Instead add:
resources.view[] = ""
And make sure that it is in the top [production] area of the application.ini
It is also worth turning on error reporting so you can see the error (and google it like I did!):
phpSettings.display_errors = 1
Fatal error: Cannot redeclare class zend_application_resource_resource. If this code worked without the Zend Optimizer+, please set zend_optimizerplus.dups_fix=1 in your php.ini in /usr/share/php/libzend-framework-php/Zend/Application/Resource/ResourceAbstract.php on line 26
I also have a problem.. I followed the complete tutorial till the end of "create layout". If I want to start my project /quickstart/public I just get the HTTP error 500 screen... what shall I do?
Also. People really should know that resources.view[] is an array. But why is an array required? Why can't I just use resources.view?
Also resources.view.doctype = "XHTML1_STRICT" seems a lot more logical to me than doing all that stuff in the bootstrap, which is also not explained why it is done, just that it is done this way.
I can't set them on the ini.
They have to be set on each module's bootstrap.
But how?
(or is there any other way that I've missed?)
Cannot redeclare class zend_application_resource_resource. If this code worked without the Zend Optimizer+, please set zend_optimizerplus.dups_fix=1 in your php.ini in /usr/share/php/libzend-framework-php/Zend/Application/Resource/ResourceAbstract.php on line 26
when i am removing the settings of layout from my project, it is again working fine.
The document root in httpd.conf is set to /var/www and I have a public folder therein where I copied index.php from my project directory, also stored on the server. I added the two lines to application.ini
resources.layout.layoutPath=APPLICATION_PATH"/layouts/scripts"
resources.view[]=""
Bootstap.php contains the function _initDocType as
$this->bootstrap($view);
$view=$this->getResource($view);
$view->doctype('XHTML1_STRICT');
/applicatoin/layouts/scripts/layout.phtml is pretty vanilla only referencing a stylesheet and having a <h1> tag. After the closing body tag, I have <?php echo $this->layout()->content; ?>
What further changes should I make to ensure that my layout is served when the home page is browsed?
Thanks,
Sid
the new action page points the css file to:
http://mysite/pubic/index/style/main.css
instead of pointing to : http://mysite/public/style/main.css
Any idea why?
Thanks in advance
resources.view[] = ""
Surely this is a temporary state of affairs and will be fixed later on in the tutorial. Right?
It looks like :
Fatal error: Call to a member function doctype() on a non-object in /var/www/quickstart/application/Bootstrap.php on line 9 Call Stack: 0.0007 629192 1. {main}() /var/www/quickstart/public/index.php:0 0.0390 2077440 2. Zend_Application->bootstrap() /var/www/quickstart/public/index.php:25 0.0390 2077520 3. Zend_Application_Bootstrap_BootstrapAbstract->bootstrap() /usr/share/php/libzend-framework-php/Zend/Application.php:355 0.0391 2077520 4. Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap() /usr/share/php/libzend-framework-php/Zend/Application/Bootstrap/BootstrapAbstract.php:582 0.0399 2078672 5. Zend_Application_Bootstrap_BootstrapAbstract->_executeResource() /usr/share/php/libzend-framework-php/Zend/Application/Bootstrap/BootstrapAbstract.php:618 0.0399 2079328 6. Bootstrap->_initDoctype() /usr/share/php/libzend-framework-php/Zend/Application/Bootstrap/BootstrapAbstract.php:665
I may be wrong. But I think this tutorial is pretty poorly constructed for a "quickstart"!
Also, not sure why I'm asked to fill in "the 5 letters that you see below." on this form when actually there are two 7 letter words shown!! Dear, oh dear.
Using this framework is like delving into a black box of trial and error 'tweaks' to the default content provided in these supposed 'help' files.
Any framework or component is essentially useless without good documentation, or examples and this documentation is an example of how to ruin a good product.
1. Create manually directories and file: application/layouts/scripts/layout.phtml
2. Go to application.ini, add two lines into [production] area and sure again that these two lines are in [production] area:
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.view[]=
3. copy code and paste to Bootstrap.php and layout.phtml file.
If your project worked correctly before, it will run.