Yii Framework official tutorial 3-Start: create the first Yii application

Source: Internet
Author: User
Tags contact form
To have a preliminary understanding of Yii, we will describe how to build the first Yii application in this section. We will use yiic (command line tool) to create a new Yii application. Gii (powerful web-based code generator) for specific tasks...



To have a preliminary understanding of Yii, we will describe how to build the first Yii application in this section. We will use yiic (command line tool) to create a new Yii application. Gii (a powerful web-based code generator) automatically generates code for specific tasks. Assume that YiiRoot is the installation directory of Yii, and WebRoot is the document root directory of the server. Run yiic on the command line as follows:

% YiiRoot/framework/yiic webapp WebRoot/testdrive

Note:When running yiic in MacOS, Linux, or Unix systems, you may need to modify the permission of the yiic file to make it run. In addition, you must be able to directly execute the php command in the terminal command line (you can use the php-v test in the terminal). If not, you need to place the executable php path in the system path. You can also run this tool as follows:

% cd WebRoot% php YiiRoot/framework/yiic.php webapp testdrive

This will create a basic Yii application under the WebRoot/testdrive directory. This application has the directory structure required by most Yii applications. Without writing a line of code, we can access the following URL in the browser to see our first Yii application:

#

As we can see, this application contains three pages: Homepage, contact page, and logon page. The home page displays information about applications and user logon statuses. the contact page displays a contact form for users to enter and submit their inquiries, the logon page allows users to pass authentication and then access authorized content. See the following for more information:

The following tree chart describes the directory structure of our application:

Testdrive/index. php Web app portal script file index-test.php function test use portal script file assets/contains public resource file css/contains CSS file images/contains image file themes/contains app topic protected/contains subject yiic command line script yiic. the yiic command line script yiic in bat Windows. php yiic command line PHP script commands/contains the custom 'yic' command shell/contains the custom 'yicic shell' command components/contains reusable user component Controller. identity of all php controller classes. the 'Identity 'class config/containing the configuration file console for php authentication. main. php Web application configuration test. the configuration controllers/class file containing the controller SiteController used for php function testing. php default controller class file data/contains sample database schema. mysql. SQL example MySQL database schema. sqlite. SQL example SQLite database testdrive. db example SQLite database file extensions/contains third-party extension messages/contains the translated message models/class file containing the model LoginForm. php 'login' action form model ContactForm. the form model runtime/contains the temporary file tests/contains the test script views/contains the controller view and layout file layouts/contains the Layout view file main. default layout of all views of php column1.php use single-column pages use layout column2.php use double-row pages use layout site/view file pages containing the 'site' controller/contain "static" page about. php "about" page view contact. php 'Contact 'Action view error. php 'error' action View (displaying external errors) index. php 'index' action View login. php 'login' action View system/system view file
1. connect to the database

Most Web applications are driven by databases, and our testing applications are no exception. To use a database, we first need to tell the application how to connect to it. Modify the application configuration file WebRoot/testdrive/protected/config/main. php as follows:


return array(    ......    'components'=>array(        ......        'db'=>array(            'connectionString'=>'sqlite:protected/data/source.db',        ),    ),    ......);

The above code tells the Yii application to connect to the SQLite database WebRoot/testdrive/protected/data/testdrive. db as needed. Note that this SQLite database is included in the application framework we created. The database only contains a table named tbl_user:


CREATE TABLE tbl_user (    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,    username VARCHAR(128) NOT NULL,    password VARCHAR(128) NOT NULL,    email VARCHAR(128) NOT NULL);

If you want to change to a MySQL database, you need to import the file WebRoot/testdrive/protected/data/schema. mysql. SQL to create a database.

Note:To use the Yii database function, we need to enable the PDO extension of PHP and the corresponding driver extension. For testing applications, we need to enable php_pdo and php_pdo_sqlite extensions.

2. implement CRUD operations

An exciting moment is coming. We want to implement the CRUD (create, read, update, and delete) operations for the created tbl_user table, which is also the most common operation in practical applications. We don't have to bother writing the actual code. here we will use Gii-a powerful Web-based code generator.

Configure Gii

To use Gii, you must first edit the file WebRoot/testdrive/protected/main. php, which is a known application configuration file:

return array(    ......    'import'=>array(        'application.models.*',        'application.components.*',    ),    'modules'=>array(        'gii'=>array(            'class'=>'system.gii.GiiModule',            'password'=>'pick up a password here',        ),    ),);

Then, access #. Here we need to enter the password, which is specified in the above configuration.

Generate User model

After logging in, click Model Generator. The following model generation page is displayed:

The above is an additional article 3 of the Yii Framework official tutorial -- start: create the content of the first Yii application. For more information, see PHP Chinese Web (www.php1.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.