PHP framework YII Note 1

Source: Internet
Author: User
If the application directory is yiidemo, then cdyiidemoyiicwebappdemo (the application name) will generate framework 2 to create contro PHP framework YII note 1.
1. install yiicin the architecture, and put the yiicand php.exe under frameworkin the PATH,
Assume that the application directory is yiidemo.
Cd yiidemo
Yiic webapp demo (this is the name of the application)
A framework will be generated later


2. create a controller
Cd yiidemo
Yiic shell
Now you can help to see help
Controller generated first
Controller message (controller name) helloworld (method name)
The controller is also generated under the protectd directory by default, and the views directory also has the same controller
Views file with the same name

Run:
Http: // localhost: 8082/myphp/yiidemo/index. php? R = message/helloWorld

Passing parameters to a view is the same:
$ TheTime = date ("d m j G: I: s T Y ");
$ This-> render ('helloworld', array ('time' => $ theTime ));
VIEW:



3 yii HTML Help class connection:

The output will be a LINK. The first parameter is the connected text, and the 2nd arrays are
Key-value: controller/method

4 yii unit test
The test file is stored in protected/tests/unit /.
It is generally named after Action + Test, such as MessageTest. class.
If the method to be tested is actionHelloworld (), the test method is
TestActionHelloworld ().

Function test files are stored in protected/tests/functional.

5 Unit test procedure
1) install phpunit
Pear channel-discover pear.phpunit.de
Pear install phpunit/PHPUnit

(Upgrade pear upgrade-all)
2) Download selenium RC SERVER
3) modify WebTestCase. php under the test directory
Define ('test _ BASE_URL ', 'http: // localhost: 8082/myphp/yiidemo/index-

Test. php /');
4) Start selenium server

5) Test start
Cd protected/tests/
Phpunit functional/SiteTest. php
IE is automatically opened and FIREFOX is tested. you can modify the browser settings in test \ phpunit. xml.

6) TDD
Every time you write a class
Phpunit/XXXX. PHP test

Example
Yii: import ('application. controllers. MessageController ');
Class MessageTest extends CTestCase
{
Public function testRepeat ()
{

$ Message = new MessageController ('messagetest ');
$ Yell = "Hello, Any One Out There? ";
$ ReturnedMessage = $ message-> repeat ($ yell );
$ This-> assertEquals ($ returnedMessage, $ yell );
}
}

?>

7. database connection
$ Connection = new CDbConnection ($ dsn, $ username, $ password );
SQLite: sqlite:/path/to/dbfile
? MySQL: mysql: host = localhost; dbname = testdb
? PostgreSQL: pgsql: host = localhost; port = 5432; dbname = testdb
? SQL Server: mssql: host = localhost; dbname = testdb
? Oracle: oci: dbname = // localhost: 1521/testdb
The configuration file is in
/Protected/config/main. php
Modify to MYSQL configuration
// Application components
'Components' => array (
...
'DB' => array (
'Connectionstring' => 'MySQL: host = 127.0.0.1; dbname = trackstar_dev ',
'Default' => true,
'Username' => 'Your _ db_user_name ',
'Password' => 'Your _ db_password ',
'Charset' => 'utf8 ',
),
),
Use Time: Yii: app ()-> db


8. start GII
Gii is one of the tools for quick building. it is actually a tool for generating scaffolding for web pages.
1) configuration
Protected \ config \ main. php
Added:
'Modules' => array (
'Giii '=> array (
'Class' => 'system. gii. GiiModule ',
'Password' => '[self-defined password]',
),
),


2) create a table in the database and generate a MODEL class in GII.
3) continue unit test
Class ProjectTest extends CDbTestCase
{
Public function testCRUD ()
{
// Create a new project
$ NewProject = new Project;
$ NewProjectName = 'Test Project 1 ';
$ NewProject-> setAttributes (
Array (
'Name' => $ newProjectName,
'Description' => 'test project number one ',
'Create _ time' => '2017-01-01 00:00:00 ',
'Create _ user_id '=> 1,
'Update _ time' => '2017-01-01 00:00:00 ',
'Update _ user_id '=> 1,
)
);
$ This-> assertTrue ($ newProject-> save (false ));
}
}
?>
The false parameter passed by save indicates that the validity is no longer verified during storage.
Test read:
// READ back the newly created project
$ RetrievedProject = Project: model ()-> findByPk ($ newProject-> id );
$ This-> assertTrue ($ retrievedProject instanceof Project );
$ This-> assertEquals ($ newProjectName, $ retrievedProject-> name );
$ NewProject-> id: find its ID primary key
Update and delete
// UPDATE the newly created project
$ UpdatedProjectName = 'updated Test Project 1 ';
$ NewProject-> name = $ updatedProjectName; $ this-
> AssertTrue ($ newProject-> save (false ));
// Read back the record again to ensure the update worked
$ UpdatedProject = Project: model ()-> findByPk ($ newProject-> id );
$ This-> assertTrue ($ updatedProject instanceof Project );
$ This-> assertEquals ($ updatedProjectName, $ updatedProject-> name );
// DELETE the project
$ NewProjectId = $ newProject-> id;
$ This-> assertTrue ($ newProject-> delete ());
$ DeletedProject = Project: model ()-> findByPk ($ newProjectId );
$ This-> assertEquals (NULL, $ deletedProject );


Then use the crud generator of the scaffold to generate CRUD.




9 validator
YII has many validator values. usage:
Public function rules ()
{
// NOTE: you shocould only define rules for those attributes that
// Will receive user inputs.
Return array (
Array ('create _ user_id, update_user_id ', 'numerical', 'integero
Nly '=> true ),
Array ('name', 'length', 'Max' => 128 ),
Array ('create _ time, update_time ', 'Safe '),
// The following rule is used by search ().
// Please remove those attributes that shocould not be searched.
Array ('Id, name, description, create_time, create_user_id,
Update_time, update_user_id ', 'Safe', 'on' => 'Search '),
Array ('name', 'required '),
);
}
'On' => 'insert' and so on, it means that the rule is suitable for use in the case of insert.
Array ('name', 'field', 'Field 2' ...... 'required '),


10 create TEST FIXTURE
1) protected/tests/fixtures/tbl_project.php, all files are in

Create under protected/tests/fixtures
For example
Return array (
'Project1' => array (
'Name' => 'test Project 1 ',
'Description' => 'This is test project 1 ',
'Create _ time' => '',
'Create _ user_id '=> '',
'Update _ time' => '',
'Update _ user_id '=> '',
),
'Project2' => array (
'Name' => 'test Project 2 ',
'Description' => 'This is test project 2 ',
'Create _ time' => '',
'Create _ user_id '=> '',
'Update _ time' => '',
'Update _ user_id '=> '',
),
'Project3 '=> array (
'Name' => 'test Project 3 ',
'Description' => 'This is test project 3 ',
'Create _ time' => '',
'Create _ user_id '=> '',
'Update _ time' => '', 'update _ user_id '=> '',
),
);
Then, create the following in tests/unit/ProjectTest. php:
Class ProjectTest extends CDbTestCase
{
Public $ fixtures = array
(
'Project' => 'project ',
);
}
Tell us to use the previous fixture.
Use test data
$ ProjectOne = $ this-> projects ['project1'];


11) use the test database during testing
In protected/config/test. php, set as follows:
Return CMap: mergeArray (
Require (dirname (_ FILE _). '/main. php '),
Array (
'Components' => array (
'Fixture '=> array (
'Class' => 'system. test. cdbfixturemanager ',
),
'DB' => array (
'Connectionstring' =>
'MySQL: host = localhost; dbname = trackstar_test ',
'Default' => true,
'Username' => '[your db username]',
'Password' => '[your db password]',
'Charset' => 'utf8 ',
),
),
)

);

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.