Yii Framework Official Tutorial Supplement 3--start: Create the first Yii application

Source: Internet
Author: User
Tags contact form



In order to have a preliminary understanding of yii, we describe how to build the first Yii application in this section. We will use the YIIC (Command line tool) to create a new YII application. The GII (powerful Web-based code generator) automates code generation for specific tasks. Assuming that Yiiroot is the installation directory for YII, WebRoot is the server's document root directory. Run YIIC at the command line as follows:

% YIIROOT/FRAMEWORK/YIIC WebApp webroot/testdrive

Note: when running YIIC on MacOS, Linux, or Unix systems, you may need to modify the permissions of the Yiic file to enable it to run. And you have to be able to execute the PHP command directly on the terminal command line (you can use the PHP-V test in the terminal), and if not, you need to put the executable PHP path in the system path. In addition, you can run this tool as well:

% CD webroot% php yiiroot/framework/yiic.php webapp testdrive

This will create the most basic YII application in the Webroot/testdrive directory. This application has the required directory structure for most YII applications. Instead of writing a single line of code, we can access the following URL in our browser to see our first YII application:

http://www.php.cn/

As we can see, this app contains three pages: Home page, contact page, landing page. Home Show some information about the application and user login status, the contact page displays a contact form for users to fill out and submit their inquiries, and the login page allows the user to authenticate and then access the authorized content. See below for more information:

The following tree diagram depicts the directory structure of our application:

testdrive/index.php Web App Portal script file index-test.php function test the entry script file used assets/ Contains the public resource file css/contains the CSS file images/contains the picture file themes/contains the app theme PR otected/contains protected application files YIIC YIIC command-line scripts yiic.bat YIIC command-line feet Under Windows This yiic.php YIIC command line PHP script commands/contains custom ' YIIC ' commands shell/contains the        The ' yiic Shell ' command defined components/contains a reusable user component controller.php the underlying class of all controller classes identity.php          The ' Identity ' class for authentication config/contains configuration files console.php Console app configuration main.php Web app configuration The test.php feature test uses a configuration controllers/class file that contains the Controller's class files sitecontroller.php the default controller da ta/contains sample database Schema.mysql.sql sample MySQL database schema.sqlite.sql example SQLite database testdrive. DB Example SQLite database textPiece extensions/contains a third-party extension messages/contains the translated message models/the class file that contains the model Lo      ginform.php ' Login ' action form model contactform.php ' contact ' Action form model runtime/contains temporary generated files tests/contains the test script views/contains the controller's view and layout file layouts/contains the layout view file M               ain.php the default layout for all views column1.php use the layout used by a single-column page column2.php the layout used for pages using two columns site/            View file containing ' site ' controller pages/view with "static" page about.php "about" page contact.php ' Contact ' Action View error.php ' error ' action view (show external error) index.php ' index ' move View of login.php ' login ' action system/contains system view file

1. Connect to a database

Most WEB applications are database-driven, and our test applications are no exception. To use a database, we first need to tell the app how to connect to it. Modify the application's 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 when needed. Note that this SQLite database is already included in the application framework we created. The database contains only one table named Tbl_user:


CREATE TABLE tbl_user (    ID INTEGER not NULL PRIMARY KEY AutoIncrement,    username VARCHAR ($) NOT NULL,    PASSWO RD VARCHAR ($) NOT NULL,    email VARCHAR (+) not NULL);

If you want to switch to a MySQL database, you need to import the file Webroot/testdrive/protected/data/schema.mysql.sql to build the database.

Note: to use YII's database functionality, we need to enable the PHP PDO extension and the corresponding driver extension. For test applications, we need to enable PHP_PDO and Php_pdo_sqlite extensions.

2. Implementing CRUD Operations

The exciting moment has come. We want to implement CRUD (create, read, update, and delete) operations for the Tbl_user table we just created, which is the most common operation in practical applications. We don't have to bother writing the actual code, here we'll use gii--a powerful web-based code generator.

Configuration Gii

In order to use the GII, you first need to edit the file webroot/testdrive/protected/main.php, which is a known app 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 http://www.php.cn/. Here we need to enter the password, which is specified in our configuration above.

Generating the User model

After landing, click on the link model Generator. It will display the following model generation page:

In the table name input box, enter Tbl_user. In the Model Class input box, enter User. Then click the Preview button. This will show you the new file that will be generated. Now click the Generate button. A name of user.php will be generated into the Protected/models directory. As we'll describe later, the user model class allows us to access the data table Tbl_user in an object-oriented manner.

Generate CRUD Code

After creating the model class, we will generate code to perform CRUD operations. We select the Crud Generator in the Gii as follows:

In the Model class input box, enter user. In the Controller ID input box, enter user (lowercase format). Now click the Preview button after the Generate button. The CRUD code generation is complete.

accessing CRUD pages

Let's look at the results and visit the following URLs:

http://www.php.cn/

This displays a list of the records in the Tbl_user table. Click on the Create User link on the page and we will be taken to the login page if you are not logged in. After logging in, we see a form that allows us to add new users. Complete the form and click the Create button, and if there are any input errors, a friendly error message will appear and prevent us from saving. Back to the User list page, we should be able to see that the user just added appears in the list. Repeat the steps above to add more users. Note that if there are too many user entries on one page, the list page will be paged automatically. If we use Admin/admin as an administrator login, we can view the user Management page at the following URL:

http://www.php.cn/

This displays a nice table with user entries. We can sort the corresponding column by clicking on the cell Glyd of the header, and it will be automatically paginated as the list page. Implement all of these features don't let us write a line of code!


The above is the official Yii Framework Tutorial Supplement 3--Start: To create the first Yii application 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.