YAF 0 Basic Learning Summary 3-hello YAF

Source: Internet
Author: User

YAF 0 Basic Learning Summary 3-hello YAF

The last time we have learned how to install YAF, ready to work well after we come to the actual coding, code farmers know a classic statement is "Hello World", today we began to YAF, the goal as long as can get "Hello Yaf" on it, not much said, Here's the beginning. (Note: It is necessary for the reader to have a certain MVC knowledge base, preferably using some other simple framework)

1. Organization Directory Structure

For the use of the framework of this is very good understanding, for the use of the framework of the proposed not to die in this, first remember, the simple point is the structure of our documents to make some rules, so that you and others can see clearly, can quickly find the required files or classes.

The authors say that for YAF applications, they should follow a directory structure similar to the following. Let's look at a typical YAF directory structure
+ Public  |-index.php//import Files  |-htaccess//rewrite rules      |+ CSS  |+ img |+ js+ conf |-  application.ini//matching File   + application  |+ Controllers     |-index.php//Default controller  |+ views         |+ Index   //Controller        | index.phtml//default view  |+ modules//other modules  |+ Library//local class library  |+ models//model  directory  |+ plugins//Plugin Directory


2. Entry file

The framework has to have a unified entry file, the entry file is the entry for all requests, generally with the aid of the rewrite rule, all requests are redirected to the portal file.

Let's start with a simple copy of the code, followed by one by one to explain, below a typical entry file public/index.php
<?phpdefine ("App_path",  Realpath (dirname (__file__). ‘/.. /‘)); /* point to the upper level of public *  /$app = new Yaf_application (App_path. "/conf/application.ini"); $app->run ();


3. URL rewriting

We need to configure the URL rewrite for the server because we want all requests for the site to be in the portal file that we specify and then dispatched by the portal file to the module, controller, and action as requested by the URL.

The following is a list of common rewrite rules for apache,nginx,lighttpd,sae, which we can configure based on the Web server we use

Apache Rewrite (httpd.conf) #.htaccess, of course, can also be written in Httpd.confrewriteengine Onrewritecond%{request_filename}! Frewriterule. * Index.php//nginx's rewrite (nginx.conf) server {  Listen * * *;  server_name  domain.com;  Root   document_root;  Index  index.php index.html index.htm;   Location  /{         index public/index.php;        if (!-f $request _filename) {            rewrite ^/(. *)/public/index.php?$1 last;}}   } LIGHTTPD's rewrite (lighttpd.conf) $HTTP ["host"] =~ "(www.)? domain.com$ "{  url.rewrite = (     " ^/(. +)/?$ "="/index.php/$1 ",}//sae rewrite  (CONFIG.YAML) Name:your_app_nameversion:1handle:    -Rewrite:if (!is_dir () &&!is_file () && Path ~ "^ (. *) $") Goto "/index.php"


Remember: After modifying the configuration file, remember to restart webserver!


4. Configuration Files

In Yaf, the framework encapsulates a configuration class for us, where we can just touch it first without knowing him, just remember and imitate.

In Yaf, the configuration file supports inheritance and supports sub-sections. and support for the constants of PHP. You don't have to worry about the configuration file too large to cause parsing performance problems, because YAF will load the configuration file at the first run and keep the formatted content in memory. The configuration file will not be loaded again until it has been modified.

A simple configuration file Application/conf/application.ini
[Product]; support for directly writing defined constants in PHP application.directory=app_path "/application/"


5. Controller
In Yaf, the default module/Controller/action is named by index, which is, of course, modified by the configuration file.

For the default module, the controller directory is under the Controllers directory in the application directory, and the Action naming convention is "name +action"

Default Controller application/controllers/index.php
<?phpclass Indexcontroller extends Yaf_controller_abstract {public   function indexaction () {//default action       $ This->getview ()->assign ("Content", "Hello Yaf");}   ? >


6. View File

YAF supports a simple view engine and enables users to customize their own view engine, such as Smarty. For the default module, the path to the view file is in the directory with the lower case action name in the Views directory under the application directory.

A view of the default action application/views/index/index.phtml


OK, a basic test has been deployed, then to verify it, directly in the browser input http://www.yourhostname.com/application/index.php



Is there a "Hello Yaf" output? I believe that we have a step-by-step imitation has been successful, did not succeed to see if there are any error tips, specific analysis of the problem ha!

Reference Source:
YAF 0 Basic Learning Summary 3-hello YAF
Http://www.lai18.com/content/407126.html

YAF 0 Basic Learning Summary 3-hello YAF

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.