Introduction to PHP's YII framework tutorial using the YII Framework Tutorial _php Tutorial

Source: Internet
Author: User
Tags configuration php php framework contact form

Tutorial on Getting Started with the YII Framework for PHP, using the YII framework Tutorial


Installation

The installation of YII consists of the following two steps:

Download the YII framework from yiiframework.com.
Extract the YII compression package to a WEB-accessible directory.
tip| tip: Installing in a web directory is not required, and each YII application has a portal script that only has to be exposed to the Web user. Other PHP scripts (including Yii) should be protected from WEB access, as they may be exploited by hackers.
Demand

After installing Yii you may want to verify that your server meets the requirements of Yii, just enter the following URL in the browser to access the requirements detection script:

http://hostname/path/to/yii/requirements/index.php
The minimum requirement for YII is that your WEB server supports PHP 5.1.0 or later versions. Yii is tested on the Apache HTTP server on Windows and Linux systems and should work properly on other WEB servers and platforms that support PHP 5.

Establish the first YII application

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| 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. 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://hostname/testdrive/index.php
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 feature test uses a portal script file assets/contains the exposed resource file css/ Contains a CSS file images/contains picture files themes/contains app theme protected/contains protected app files Yiic YIIC command line script Yii C.bat YIIC command line script under Windows yiic.php YIIC command line PHP script commands/contains custom ' YIIC ' command shell/package    A custom ' yiic Shell ' command components/contains a reusable user component controller.php the underlying class of all controller classes identity.php the ' Identity ' class used for authentication       config/configuration file console.php Console app configuration main.php Web app configuration test.php features tested using the Config controllers/ class file that contains the Controller's class file sitecontroller.php default controller data/contains sample database Schema.mysql.sql sample MySQL database schema.sql         Ite.sql Example SQLite database testdrive.db sample SQLite database file extensions/contains a third-party extension messages/contains translated messages models/   The form model that contains the model's class file loginform.php ' login ' action contactform.php ' contact ' action forms runtime/contains temporary generated files tests/contains test scripts  Views/contains the controller's view and layout file layouts/contains the layout view file main.php The default layout for all views column1.php use the layout used by a single-column page column2.php use the layout of a two-column page site/The view file containing the ' site ' controller pages/the "static" page about.php the "about" page Figure contact.php ' contact ' Action View error.php ' error ' action view (show external error) index.php view of ' Index ' action login.ph View system/of P ' login ' action contains system view file

Connecting 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,  Passwor D 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| 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.
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 the URL http://hostname/testdrive/index.php?r=gii. 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 shown below,

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://hostname/testdrive/index.php?r=user
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://hostname/testdrive/index.php?r=user/admin
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!

Articles you may be interested in:

    • Build PHP's YII framework and related test environment on Mac OS
    • Win7 Installing the PHP framework Yii method
    • Nginx configuration PHP Rewrite rules example of Yii and cakephp framework
    • A detailed description of the configuration and use of the log in the YII framework of PHP
    • Walkthrough yii2.0 Steps to Run in the PHP command line
    • Yii connection, modify MySQL database and PHPUnit test connection
    • PHP YII Framework Development Little tricks Model (models) Rules custom validation rule
    • PHP Yii Framework's form validation rules Daquan
    • PHP generated ICO icon based on YII framework
    • List the development advantages of the YII 2 framework for PHP

http://www.bkjia.com/PHPjc/1100314.html www.bkjia.com true http://www.bkjia.com/PHPjc/1100314.html techarticle PHP's Yii framework Getting started using the tutorial, Yii framework using the tutorial installing Yii installation consists of the following two steps: Download the YII framework from yiiframework.com. Unzip the YII compressed package to a We ...

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