Zend Framework Introductory Knowledge Point Summary _php Example

Source: Internet
Author: User
Tags php programming smarty template zend zend framework

This article summarizes and analyzes the Zend Framework entry knowledge points. Share to everyone for your reference, specific as follows:

The Zend Framework is an implementation of the MVC pattern, and getting started is almost as much as just looking at the Zend_controller zend_view section.

1.zend_controller part. The most important class is Zend_controller_front. The classic code to use it is simple:

$front =zend_controller_front::getinstance ();
$front-> setcontrollerdirectory ("./app/controllers");
$front->dispatch ();

The explanation here is that you can't use new to get a zend_controller_front, you can only call the GetInstance method to return an instance (my zend_framework is 1.01). The controllers directory I specified here is under the app folder under the WWW document root directory. In general, app should not be placed under the document root--the so-called security problem: if the configuration is not strict, the files under the WWW document root directory may be visible to the visitor. It is usually placed in a different directory, such as in the same location as the document root, you can use:

$front->setcontrollerdirectory (".. /app/controllers ")

One more "." The relative path is used here. You can specify a different directory with the full path name. Like what:

$front->setcontrollerdirectory ("E:/server/app/controllers")

This part is included in index. in PHP. In the correct configuration, any request will be redirected by the rewrite function to the $front, which is the front-end controller Zend_controller_front instance. Any processing of any certification must be processed before dispatch.

2.zend_view Common code is:

$view =new Zend_view ();
Zend_registry::set ("View". $view);

The Zend_framework recommended directory structure is used by default. Views and controllers models siblings are in the application directory. Views below are three sibling directories scrīpts helpers filters. So when you define a controller, you have to create a new directory under Scrīpts to store the template for that controller subordinate. Like the simple

Class Indexcontroller extends zend_controller_action
{
 function indexaction ()
 }
}

You have to set up a index directory inside, and create a index.phtml template in the index directory. If a function addaction () is built under Indexcontroller, you will have to add a new add.phtml at index. You have another usercontroller, you have to have a corresponding user directory under Scrīpts. These *.phtml files are similar to HTML files and define how your output is displayed. The simple thing is to keep the blanks. But just can not, otherwise will be prompted to say "error" Invalid Controller ... This is because the default errorcontroller is already registered. The default errorcontroller is invoked when the current end controller cannot find the appropriate controller distribution.

We sometimes do not want to use the default directory structure and do not want to use the default phtml type of view template. Then we can use

$view->setparam ("Noviewranderer", true);

To cancel the default phtml type directory setting. Use

$view->setparam ("Noerrorhandler", true);

To write in the default Errorcontroller. Use

$view->setscrīptpath ("./app/views");

To set the location of your template. This is useful when using the Smarty template.

You can write this:

$view =new Zend_view_smarty ();
$view->setparam ("Noviewranderer", true);
$view->setparam ("Noerrorhandler", true);
$view->setscrīptpath ("./app/views");
Zend_registry::set ("View". $view);

This can be obtained when used:

$view =zend_registry::get ("View");

3. When the first contact with the model can be simply understood as data objects, for the operation of the database can be directly inherited zend_db_table This class encapsulation is very good, general use enough:

Class Data extends zend_db_table
{
 protected $_name= "data";
}

You only need to specify the name of the table, of course you can not display the specified, then ZENDF will default to your class name to find the table in the database. Don't forget to specify Defaultadapter for zend_db_table:

$config = new Zend_config_ini ('./application/config.ini ', ' General ');
$db = Zend_db::factory ($config->db->adapter, $config->db->config->toarray ());
Zend_db_table::setdefaultadapter ($DB);

Config.ini similar to [general]

Db.adapter = pdo_mysql
db.config.host = localhost
db.config.username = Rob
Db.config.password = 123456< C13/>db.config.dbname = Zftest

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with your PHP programming based on the Zend Framework.

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.