PHP Development Framework YII Framework Tutorial (1) First app Hello World

Source: Internet
Author: User
Tags php class sqlite database yii
The Yii Framework tutorial can be found in the official Chinese documentation, so why write this development tutorial? The purpose of this tutorial is to help Windows desktop apps or ASP. NET programmers get a quick grasp of the PHPYII Framework application framework through a different perspective, primarily by developing Windows app c++,c# programmers. One important benefit of developing Web applications using PHP is that they can be applied to a variety of operating systems (Unix,windows,mac OS), unlike ASP. The use of Php+apache+mysql (Xmap/lamp) can almost beat the invincible hand:-).

The operating system used for this tutorial is windows, and the IDE is developed VS. PHP, this development environment is used because vs.php uses Visual Studio as the IDE and is familiar to Visual Studio developers. It can also be used to develop debug c#,php applications at the same time. The Yii Framework itself is independent of the IDE, and you can use your favorite PHP development tools to develop YII applications (such as Eclipse). An introduction to vs.php can be found in VS. php + yiiframework combination to develop PHP applications. This tutorial on Yii framework knowledge is not related to the development of the IDE, can be applied to a variety of development environments, you can choose your own favorite development environment.

Between the creation of the first application, you need to download the Yii development package, download can go to the Yii website http://www.yiiframework.com/download/download, the current version is 1.1.12, after download, extract it directly to the C: root directory for your convenience:


Another benefit of using vs.php is that it comes with an Apache Web server, so you can install Xamp (apache+mysql+php) without having to install it separately, but you'll need to install MySQL if you want to use MySQL.

After installing the YII framework, we use vs.php to create the first PHP application, Hello world.

Vs. PHP creates the HelloWorld project and adds a index.php file.

At this point, modify index.php to

<?php  print "Hello, world!";    ? >
<?php  print "Hello, world!";    ? >

Press F5 to run the program, VS. PHP opens the browser, showing "hello,world! ”. But this is not a YII application!!! , we have not yet used the YII Web application framework, and the YII framework is a purely object-oriented application framework. The application class for its web program is the Cwebapplication class. And the MVC model is adopted.

The static structure of YII application is presented.

Shows a typical workflow for a YII application to handle user requests

The user sends a request to access the URL http://www.example.com/index.php?r=post/show&id=1, and the WEB server processes the request by executing the portal script index.php.

The portal script creates an application instance and executes it.

The application obtains the details of a user request from an application component called request.

The application is used with the help of an application component called Urlmanager, which determines the requested controller and action. In this example, the controller is post, which represents the Postcontroller class, and the action is show, whose actual meaning is determined by the controller.

The app creates an instance of the requested controller to further process the user request. The controller determines the action show pointing to a method named Actionshow in the controller class. It then creates and holds the filters associated with the action (for example, access control, benchmark). If the filter is allowed, the action will be executed.

The action reads a Post model with ID 1 from the database.

The action renders a view named show through the Post model.

The view reads and displays the properties of the Post model.

The view executes some small objects.

The rendered result of the view is inserted into a layout.

The action finishes rendering the view and renders it to the user.

The YII application itself has many configurations, such as the controller to be saved, the action to use the log file, and so on. For the simple application of Hello,world, all default values are used. The default controller is Sitecontroller and the action is indexaction. In other words, for example, if your site is www.guidebee.com, if you use the Yii Framework, the user requests www.guidebee.com, the Yii application cwebapplication An instance of the class Sitecontroller is created and the Indexaction method of the Sitecontroller is called, which is equivalent to the main method of the program that the C # console applies.

YII projects use different directories to store controller,action,view,layout, and the default directory structure is as follows

testdrive/
index.php Web App Portal script file
Portal script files used by the index-test.php functional test
assets/contains a public resource file
css/contains CSS files
Images/contains picture files
Themes/contains app themes
protected/contains protected application files
YIIC YIIC Command Line script
Yiic.bat YIIC Command-line script under Windows
yiic.php YIIC command line PHP script
commands/contains the custom ' YIIC ' command
shell/contains custom ' YIIC shell ' commands
components/contains reusable user components
Controller.php base class for all controller classes
Identity.php ' Identity ' class for authentication
config/contains configuration files
console.php Console Application Configuration
main.php Web App Configuration
Configuration used by the test.php functional test
controllers/class file containing the controller
sitecontroller.php class file for default controller
data/contains sample Databases
Schema.mysql.sql Sample MySQL Database
Schema.sqlite.sql Example SQLite database
Testdrive.db Sample SQLite Database file
extensions/contains third-party extensions
messages/contains translated messages
models/class files that contain models
Form model for loginform.php ' login ' action
Form model for contactform.php ' contact ' action
runtime/contains files that are temporarily generated
tests/contains test scripts
views/contains the view and layout files for the controller
layouts/contains layout view files
main.php default layout for all views
column1.php using layouts for single-column pages
column2.php the layout used for a page with two columns
site/The view file containing the ' site ' controller
Pages/contains a "static" page
about.php view of the "about" page
View of contact.php ' contact ' action
error.php ' ERROR ' Action view (external error is displayed)
index.php The view of the ' Index ' action
View of login.php ' login ' action
system/contains system view files

This directory structure can create the first Yii application by creating a default file with Yii's own tool.

For the Hello World project, there is no need to be so complicated, we just need to create the protected \controllers directory to store the sitecontroller.php.

Each YII application has an entry script that can be understood as a C # program class. This portal script is similar

<?php    //contains yii boot file  //require_once (dirname (__file__). /.. /.. /framework/yii.php ');  $yii = ' c:/yiiframework/yii.php ';  When publishing the application, remove the following code to avoid generating debug information  defined (' Yii_debug ') or define (' Yii_debug ', true);    Require_once ($yii);  Create an application instance and execute    yii::createwebapplication ()->run ();
<?php    //contains yii boot file  //require_once (dirname (__file__). /.. /.. /framework/yii.php ');  $yii = ' c:/yiiframework/yii.php ';  When publishing the application, remove the following code to avoid generating debug information  defined (' Yii_debug ') or define (' Yii_debug ', true);    Require_once ($yii);  Create an application instance and execute    yii::createwebapplication ()->run ();

As mentioned earlier, the default controller for Yii is Sitecontroller, and the default action is Actionindex, so the Sitecontroller code for HelloWorld is as follows

/**  * Sitecontroller is the default controller to handle user requests.  */  class Sitecontroller extends Ccontroller  {      /**      * Index action is the default action in a controller .      *      /Public Function Actionindex ()      {          echo ' Hello world ';      }  }
/**  * Sitecontroller is the default controller to handle user requests.  */  class Sitecontroller extends Ccontroller  {      /**      * Index action is the default action in a Controller.
  */Public      function Actionindex ()      {          echo ' Hello world ';      }  }

When you run the app again, you can display "Hello,world" in your browser. Currently no MVC model is used to print "Hello,world" directly from the controller using ECHO, and the following changes the code slightly to create a simple view.

The view default directory is the views subdirectory under the protected directory, and the controller class for, for example, Sitecontroller corresponds to the site subdirectory under the Views directory, Like ASP., the Yii view (the page class corresponding to the ASP. NET) can also be used as layout with the Masterpage,yii application, and the default layout is stored in the layouts subdirectory of the views.

Modify the Actionindex method for Sitecontroller, instead:

Public Function Actionindex ()  {   $this->render ("index");  }
Public Function Actionindex ()  {   $this->render ("index");  }

View view is a PHP script that contains the main user interaction elements. He can include PHP statements, but we recommend that these statements do not change the data model, and that it is best to keep it simple (purely as a view). In order to achieve logical and interface separation, large segments of logic should be placed in the controller or model, not in the view, the view has a name, when rendering (render), the name is used to identify the view script file.

Actionindex displays a view using the Render method, corresponding to the index.php in the Views->site directory. Render defaults to use the main.php as layout (MasterPage) under Views->layouts

A layout is a special view file that is used to decorate a view. It typically contains a subset of the views common to the user interface. For example, a layout can contain the header and footer parts, and then embed the content in between.

... the header here .....
<?php echo $content;?>
..... footer here .....

The $content in it stores the rendered result of the content view.

Take a look at the index.php (view) code in the View directory:

<?php echo "hello,world!"; ?>
<?php echo "hello,world!"; ?>

This completes the Hello,world MVC model and runs the display "Hello,world".

The above is the PHP Development Framework Yii Framework Tutorial (1) The first app Hello World content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.