Zend Framework Tutorial 2-Create your project

Source: Internet
Author: User
Tags php class zend zend framework zend server

In order to create your project, you first need to download and extract the Zend Framework. Installation of the Zend Framework (two ways)

1. The simplest way to download the Zend framework with a full PHP stack is by installing Zend Server. Zend server has a local installation for Mac Osx,windows, Fedora Core and Ubuntu, and a common installation package compatible with most Linux distributions.

After installing Zend Server, the framework files can be found in the/usr/local/zend/share/zendframework directory of Mac OSX and Linux, or in Windows C:\Program files\zend\ In the Zendserver\share\zendframework folder. This include_path will already be configured to contain the Zend framework. (This approach is relatively complex, we generally do not use Zend Server)

2, or, you can» Download the latest version of the Zend Framework, while extracting the files inside; Remember you've done it.
Optionally, you can add this frame path to the subdirectory under the library in the Include_path settings in your php.ini configuration file.
In this way, the Zend Framework is already installed and ready to be used. (recommend this method) create a project

Note: ZF command-line tools
In the Zend Framework's installation directory, there is a bin/subdirectory containing zf.sh and Windows user-based Zf.bat script files based on UNIX users, remembering the absolute paths of these scripts.
Wherever you are using the command-line tool ZF, replace it with a script file that contains an absolute path. In Unix-like systems, you might want to use the alias function of the shell script: alias zf.sh=path/to/zendframework/bin/zf.sh.
If you have problems installing the command line tool on ZF, please refer to: Manual

Open a terminal (run under window, enter cmd), and go to the directory where you want to start creating the project. Then, using the appropriate script, execute the following script:

% ZF Create Project QuickStart

Note: If you do not successfully create your project according to the above procedure, OK, that's okay, look here.
Running this command will create a basic site structure directory, including the initial controller and view. The property directory structure is as follows:

QuickStart
|--Application
|   | --bootstrap.php
|   | --Configs
|   |   '--Application.ini
|   | --Controllers
|   |   | --errorcontroller.php
|   |   '--indexcontroller.php
|   | --Models
|   '--Views
|       | --Helpers
|       '--Scripts
|           | --Error
|           |   '--error.phtml
|           '--Index
|               '--index.phtml
|--Library
|--public
|   | --. htaccess
|   '--index.php
'--Tests
    |--application
    |   '--bootstrap.php
    |--Library
    |   '--bootstrap.php
    '--phpunit.xml

At this time, if you do not add the Zend Framework to your include_path, we recommend copying or linking the symbols to your library/directory. In either case, you want a recursive copy or a symbolic link Zend the library/zend/directory under the framework installation directory to your project directory library/directory. In a Unix-like system, you can perform any of the following:

# Symlink:
% CD library; Ln-s path/to/zendframework/library/zend.

# Copy:
% CD library; Cp-r path/to/zendframework/library/zend.

Under the Windows system, this operation is very easy to operate through under the Explorer.
Now that the project has been created, the main task is to understand the startup, configuration, action Controller, and view. Boot program (the Bootstrap)

Your bootstrap class file defines which resources and components are to be initialized. By default, the front controller of the Zend framework is initialized with application/controllers/as the default directory, and the controller is used to find the action controller (detailed later). The contents of this class are as follows:

Application/bootstrap.php

class Bootstrap extends Zend_application_bootstrap_bootstrap
{
}

As you can see, there is not much need to be done at the beginning, greatly reducing our development steps. Configuration (config)

When the Zend Framework itself is less configured, it is more about configuring our applications. By default, the configuration is saved: In the Application/configs/application.ini file, this file contains some basic PHP environment setting instructions (such as instance, open or close error report), Indicates the path to the bootstrap class file (as well as the Bootstrap class name) and the path of the action controller. The content might look like this:

; 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 "
appnamespace = "Application"
resources.frontController.controllerDirectory = Application_path "/controllers"
resources.frontController.params.displayExceptions = 0

[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 you use Ini-style's configuration file, you can directly reference constants and rewrite them; Application_path is usually a constant. Also note that there are several parts defined: production, staging, testing, and development. The following three are the configurations that inherit from production. This is a very useful way to organize configuration files to ensure that the appropriate property settings are valid at each stage of application development. Action Controller (Action Controllers)

Your application's action controller contains your application's workflow and maps your requests to the appropriate patterns and views to do the work.

An action controller should contain one or more methods that end with "Action", which may be initiated by the network. By default, the Zend framework's URLs follow this pattern:/controller/action, where the "controller" is mapped to the action controller name (minus the "controller" suffix), the "action" is mapped to an action method (minus the "action" suffix).

Typically, you need a indexcontroller, which is a back controller that also serves the home page of the site, and requires a errorcontroller to indicate something wrong, such as an HTTP 404C error ( Controller or action not found) and HTTP 500 error (appliction error).

The default controller Indexcontroller is as follows:

Application/controllers/indexcontroller.php

class Indexcontroller extends Zend_controller_action
{

    Public function init ()
    {/
        * Initialize Action Controller here *
    /} public

    function indexaction ()
    { c8/>//action Body
    }
}

The default Errorcontroller are as follows:

//application/controllers/errorcontroller.php Class Errorcontroller extends Zend_controller_action {public Function erroraction () {$errors = $this-&G

        T;_getparam (' Error_Handler '); Switch ($errors->type) {case Zend_controller_plugin_errorhandler::exception_no_route:case Zen D_controller_plugin_errorhandler::exception_no_controller:case zend_controller_plugin_errorhandler::exception _no_action://404 ERROR--controller or ACTION not found $this->getresponse ()->se
                Thttpresponsecode (404);
                $this->view->message = ' Page not found ';
            Break
                Default://Application Error $this->getresponse ()->sethttpresponsecode (500);
                $this->view->message = ' Application error '; Break 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.