I originally learned from an older version of a tutorial called Getting Started with the Zend Framework [pdf] from Akra's DevNotes, so I will follow his lead with a few stylistic choices. You may want to check out that tutorial or a newer version of it if it's out.
This is just a bare-bones setup. Nothing fancy. No databases, forms, etc. Just "hello world" to get you started. You'll want to get this working before moving on, of course.
- Zend Library
I'm using version 0.7.0. Unzip ZF and copy the library directory (ZendFramework-0.7.0/library/) into your webroot so that you have /webroot/library/. This directory should contain Zend.php and a Zend subdirectory. This is really all you need to start using Zend, but there are a few standard things that still need to be done for a website. - PHP Config
You'll want to make sure the following two php.ini settings are set.
register_globals = Off
magic_quotes_gpc = Off
If you're using PHP as an Apache module, remember to restart Apache for changes to take effect. - Apache Config
Right now, anyone can navigate to or request a file in your library directory. To prevent this, use a .htaccess file in the library directory, /webroot/library/, with the following directive. You will also want to copy this into your application directory, /webroot/application/, where you store all your application-specific MVC files.
deny from all - Bootstrapping
Bootstrapping allows all URL requests to be processed through a single entry-point, called the bootstrap file. This way, URLs can be mapped to controller actions. In /webroot/.htaccess, add the following lines which internally redirect all URLs to the index.php file.
RewriteEngine on
RewriteBase /
RewriteRule .* index.php
However, certain files like client scripts, style sheets, and images should be served as normal without involving the framework. So in your public directory, /webroot/public/, where such files are stored, add the following in another .htaccess file.
RewriteEngine off
If you're using phpMyAdmin in a subdirectory on a local server, you'll want to do the same for that directory to serve files as usual.
Now /webroot/index.php is the bootstrap file and the entry-point for all requests, besides in the /webroot/public/ directory. - Standard Bootstrap File and Dispatching
The following is a bare-bones bootstrap file that uses standard routing. Put this in /webroot/index.php.setControllerDirectory('./application/controllers');Leaving out the closing ?> is intentional since including it in pure PHP files can lead to unwanted output and hard-to-detect bugs.
// Turn on display of exceptions.
$frontController->throwExceptions(true);
// Dispatch and run.
$frontController->dispatch(); - Index Controller and Standard Routing
To set up a simple index page, create a new file /webroot/application/controllers/IndexController.php and add the following.in IndexController::indexAction()';
}
function fooAction()
{
echo 'in IndexController::fooAction()
';
}
}
Now, visiting http://hostname/ should display a page with "in IndexController::indexAction()", and visiting http://hostname/index/foo/ should display a page with "in IndexController::fooAction()". Of course, due to the way the default router works, all the following URLs display the same thing.
http://hostname/
http://hostname/index
http://hostname/index/
http://hostname/index/index
http://hostname/index/index/
The default router looks at a URI and matches against the pattern /controller/action/param1/value1/param2/value2. http://hostname/ defaults to the index action of the index controller, which will be identical to http://hostname/index/index. This is also identical to http://hostname/index/index/ because the default router trims leading and trailing slashes. (Curiously, http://hostname/index returns a 400 Bad Request error on my shared hosting site; I'm not sure why. I never noticed this before.)
If you're having the problem of navigating to http://hostname/index/index failing (returning a 404) even when http://hostname/ works — a problem I ran in to working on a shared host — you must make a dummy /webroot/index/ directory with a .htaccess file telling Apache to use the rewrite router. In other words, create the directory /webroot/index/, and copy /webroot/.htaccess into it. This must be done for each subdirectory (equivalently, each controller) that you wish to have. So if you want /downloads/ to be a valid URI, you must make a /webroot/downloads/ directory. If you want /archives/ to be a valid URI, you must make a /webroot/archives/ directory, and so on. Each with the .htaccess file specifying the RewriteRule. The cause for this has to do with Apache's configuration. However, I haven't been able to determine the exact reason since the shared host I'm using does not allow access to the config file.
Installing Zend Framework, hosted by Mitchell Hashimoto (11:34)
Note: You may need to use use headphones or an amplified speaker setup for best sound reproduction. Make sure that your speakers are unmuted!
Bootstrapping, hosted by Mitchell Hashimoto (9:17)
Note: In this and the subsequent "Getting Started" screencasts, the public directory has been renamed to demo_app. They are the same directory for the purposes of these demos and you can use whichever you prefer. In summary: demo_app == public and is the "document root" directory where your publicly accessible files should be stored.
Controllers and Views, hosted by Mitchell Hashimoto (11:37)
Extending your ZF application, hosted by Mitchell Hashimoto (8:06)
This is the last screencast in the "Getting Started" series.
No comments:
Post a Comment