Zend Framework Introductory Knowledge Point summary _php instance

Source: Internet
Author: User
Tags smarty template zend framework
This paper summarizes and analyzes the Zend framework getting started knowledge point. Share to everyone for your reference, as follows:

Zend Framework is an implementation of the MVC pattern, to get a quick introduction almost only to see Zend_controller zend_view part of it.

1.zend_controller section. The most important class is Zend_controller_front. The classic code that uses it is simple:

$front =zend_controller_front::getinstance (), $front-Setcontrollerdirectory ("./app/controllers"); $front Dispatch ();

The point here is that you can't use new to get a zend_controller_front, only call the GetInstance method to return an instance (my Zend_framework uses 1.01). The controllers directory I specified here is under the app folder under the WWW document root directory. In general, apps should not be placed at the root of the document-the so-called security issue: 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, and can be used:

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

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

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

This section is included in index. in PHP. Under the correct configuration, any request will be redirected from the rewrite function to the $front, which is the front-end controller Zend_controller_front instance. All authentication and processing should be processed before dispatch.

2.zend_view Common code is:

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

By default, the directory structure recommended by Zend_framework is used. That is, views and controllers models siblings are in the application directory. Views below are three siblings of scrīpts helpers filters. So when you define a controller, you have to create a new directory underneath the scrīpts to hold the template for that controller's subordinate. Like a simple

Class Indexcontroller extends zend_controller_action{function indexaction () {}}

You'll have to create an index directory in it, and create a index.phtml template in the index directory. If you build another function Addaction () under Indexcontroller, you'll have to create a new add.phtml at index. You have another usercontroller, you will have a corresponding user directory under Scrīpts. These *.phtml files are similar to HTML files and define how you display the content of your output. The simple thing is to keep the blank. But just can't have no, otherwise will prompt say "error" Invalid Controller ... This is because the default errorcontroller is already registered. The default errorcontroller is called when the current end controller cannot find the appropriate controller distribution.

Sometimes we don't want to use the default directory structure and don't want to use the default phtml type of view template. At this point 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 up where your templates are located. This is useful when using the Smarty template.

It can be written like 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 model can be easily 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_MYSQLdb.config.host = Localhostdb.config.username = Robdb.config.password = 123456db.config.dbname = Zftest

More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

It is hoped that this article will help you to design PHP based on the Zend Framework framework.

  • Related Article

    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.