Web Frameworks - Workbook - Week 02
Main Page >> Web Frameworks >> Workbook >> Workshop - week 01
Install Zend Framework
Zend is already installed on mi-linux, so you don't have to worry about that... However if you wish to install it at home, please see this page
Create Your Project
Open a terminal (command window if you are working on Linux, Putty if you are working from Windows). Navigate to a directory where you would like to start a project (in this case your public_html folder). Then execute the following:
% zf.sh create project quickstart
Note: "quickstart" is the name of your project... you can call it anything you like! (but avoid spaces please).
Running this command will create your basic site structure, including your initial controllers and views. The tree looks like the following:
quickstart |-- application | |-- Bootstrap.php | |-- configs | | `-- application.ini | |-- controllers | | |-- ErrorController.php | | `-- IndexController.php | |-- models | `-- views | |-- helpers | `-- scripts | |-- error | | `-- error.phtml | `-- index | `-- index.phtml |-- library |-- public | `-- index.php `-- tests |-- application | `-- bootstrap.php |-- library | `-- bootstrap.php `-- phpunit.xml
One last thing before we can start working: you need to set the permissions so you can browse to your files from a web browser... this command will do the trick:
chmod 755 quickstart -R
Checkpoint
Your project is now created and accessible: http://mi-linux.wlv.ac.uk/~0123456/quickstart/public/
(obviously replace "0123456" by your own student number, and "quickstart" by your own project name!)
Your homepage should look like this:
Obviously it's not doing much just yet, but it's a start! Now let's try and understand how it all works.
The Bootstrap
Your Bootstrap class defines what resources and components to initialize. By default, Zend Framework's Front Controller is intialized, and it uses the application/controllers/ as the default directory in which to look for action controllers (more on that later). The class looks like the following:
// application/Bootstrap.php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { }
As you can see, not much is necessary to begin with.
Configuration
While Zend Framework is itself configurationless, you often need to configure your application. The default configuration is placed in application/configs/application.ini, and contains some basic directives for setting your PHP environment (for instance, turning error reporting on and off), indicating the path to your bootstrap class (as well as its class name), and the path to your action controllers. It looks as follows:
; application/configs/application.ini [production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 includePaths.library = APPLICATION_PATH "/../library" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" [staging : production] [testing : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1
Several things about this file should be noted. First, when using INI-style configuration, you can reference constants directly and expand them; APPLICATION_PATH is actually a constant. Additionally note that there are several sections defined: production, staging, testing, and development. The latter three inherit settings from the "production" environment. This is a useful way to organize configuration to ensure that appropriate settings are available in each stage of application development.
Action Controllers
Your application's action controllers contain your application workflow, and do the work of mapping your requests to the appropriate models and views.
An action controller should have one or more methods ending in "Action"; these methods may then be requested via the web. By default, Zend Framework URLs follow the schema /controller/action, where "controller" maps to the action controller name (minus the "Controller" suffix) and "action" maps to an action method (minus the "Action" suffix).
Typically, you always need an IndexController, which is a fallback controller and which also serves the home page of the site, and an ErrorController, which is used to indicate things such as HTTP 404 errors (controller or action not found) and HTTP 500 errors (application errors).
The default IndexController is as follows:
// application/controllers/IndexController.php class IndexController extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ } public function indexAction() { // action body } }
And the default ErrorController is as follows:
// application/controllers/ErrorController.php class ErrorController extends Zend_Controller_Action { public function errorAction() { $errors = $this->_getParam('error_handler'); switch ($errors->type) { case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER: case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: // 404 error -- controller or action not found $this->getResponse()->setHttpResponseCode(404); $this->view->message = 'Page not found'; break; default: // application error $this->getResponse()->setHttpResponseCode(500); $this->view->message = 'Application error'; break; } $this->view->exception = $errors->exception; $this->view->request = $errors->request; } }