search
HomePHP FrameworkYIIGet started using the yii framework

Get started using the yii framework

Nov 01, 2019 pm 03:47 PM
yiigoodframe

Get started using the yii framework

Yii installation and basic configuration

Yii is a high-performance PHP framework suitable for developing WEB 2.0 applications. You can check the features of Yii on the Yii Chinese official website (see foreign websites as well). The installation of Yii is very simple. The official provides direct download. After downloading, you can use it as long as you have a local PHP environment. You can click here to download the official Yii. In China, there may be problems with slow downloads or download failures. I have uploaded a yii-basic version. If you need it, you can click on the corresponding version to download.

Recommended study: "chuanzhipod Yii development large mall project video tutorial"

After downloading, unzip to the php environment path, I am Wampserver is installed locally, so put it directly in the site path corresponding to wampserver. After decompressing, first check whether the local PHP environment is suitable for running this version of YII. You can view the requirements.php file in the basic directory in the browser:

Get started using the yii framework

See the above prompts , indicating that the local php version supports the operation of this Yii version.

Then visit: http://localhost/basic/web/index.php. If you install it for the first time, the following error will appear

yii\web\Request::cookieValidationKey must be configured with a secret key.

We also need to perform relevant configuration before it can be used. Find the cookieValidationKey configured in the basic/config/web.php file (to prevent cookie attacks.):

Get started using the yii framework

Enter a random string and revisit the following http://localhost/ basic/web/index.php, get it done:

Get started using the yii framework

Usage of controller

controller is written uniformly in the controllers directory, and the first letter of the file name is capitalized, starting with controller At the end, and need to inherit the controller, I created a new HelloController.php file below:

Get started using the yii framework

For testing, we create a new action in HelloController.php

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
/**
* 
*/
class HelloController extends Controller
{
    public function actionIndex()
    {
        echo "hello world";
    }
}
?>

Then open http://localhost/basic/web/index.php?r=hello/index in the browser, and the browser can display hello world

where: hello represents which controller, index represents the control Specific operations in the server

Simple processing and storage location of session

We first modify the actionIndex above, add a session

  public function actionIndex()
    {
        $session = Yii::$app->session;
        $session->open();
        $session[&#39;id&#39;]="123456";
        echo $session[&#39;id&#39;];
        echo &#39;<br/>&#39;;       
        echo "hello world";
    }

, and then enter: http in the browser ://localhost/basic/web/index.php?r=hello/index, press F12 to open the debugging window to view:

Get started using the yii framework

There is one more PHPSESSID in the cookie, PHPSESSID What is its function? We know that the session can be used to temporarily store some data information, which is valid until the browser is closed. But how does the server distinguish between different sessions? It is through sessionID, which is PHPSESSID here. The browser will send a cookie (including PHPSESSID) to the server every time it sends a request. PHP uses PHPSESSID to distinguish each session.

So where is this PHPSESSID stored? We can open the php->php.ini file and search for session.save_path:

Get started using the yii framework

According to the above path, open D:\wamp\tmp,

Get started using the yii framework

There happens to be a file, and the file name is exactly the same as the value corresponding to the PHPSESSID above

Yii stores session data as a file on the server by default. Of course, we can also Set to save the session to the data table.

The above is the detailed content of Get started using the yii framework. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
How to add a new validation rule to a model in YiiHow to add a new validation rule to a model in YiiAug 12, 2025 am 07:46 AM

To add new validation rules in Yii, you only need to modify the rules() method of the model. 1. Open the model file such as User.php; 2. Add a new rule in the return array of the rules() method, in the format [Properties, Verifier, Options], such as ['age','integer','min'=>1,'max'=>120]; 3. You can use built-in validators such as 'required', 'email' or custom inline validators; 4. You can specify the scene or 'when' to set the conditions; 5. Finally, by calling validate() and checking getErrors() to test whether the rule takes effect. This process is complete and easy to implement.

How to build an application from scratch in YiiHow to build an application from scratch in YiiAug 12, 2025 am 06:14 AM

Install Yii2: Use Composer to run composercreate-projectyiisoft/yii2-app-basicmyapp to create a project; 2. Set up the web server: enter the project directory and run phpyiiserve to start the development server; 3. Understand the directory structure: master the core directory uses config/, controllers/, models/, views/, web/ and other core directories; 4. Configure the database: Modify the DSN, username and password in config/db.php to connect to the database; 5. Use Gii to generate code: Enable the Gii module in config/web.php, and use

How to implement a search functionality in YiiHow to implement a search functionality in YiiAug 12, 2025 am 12:11 AM

Create a search model (such as PostSearch) that inherits the autonomous model, define verification rules and implement search methods, and use ActiveDataProvider to manage query results; 2. Instantiate the search model in the controller and pass in request parameters to perform searches; 3. Use ActiveForm to build a search form in the view, display the results through GridView, set filterModel to enable column filtering; 4. Add public attributes to the associated fields (such as author_name) in the search model, and associate query through joinWith; 5. Optionally expand the filter logic, support date range, pull-down filtering, etc. This method uses Yii2 components to achieve high

How to optimize database queries in YiiHow to optimize database queries in YiiAug 11, 2025 pm 01:42 PM

To optimize database query performance, you must first ensure that the database design is reasonable, add indexes for columns involved in WHERE, JOIN, ORDERBY and GROUPBY, use composite indexes and avoid excessive indexing; 2. Use Yii's query caching function to cache frequently read and have fewer changes through the cache() method to reduce database access; 3. Optimize the use of ActiveRecord, avoid SELECT*, select only necessary fields, use asArray() to reduce memory overhead, and avoid N 1 query problems through with(); 4. Use joinWith() for complex queries or directly use createCommand() to execute native SQL for higher performance; 5.

How to perform acceptance testing in YiiHow to perform acceptance testing in YiiAug 11, 2025 am 11:36 AM

Install and configure Codeception, use composerrequire--devcodeception/codeception and run bootstrap initialization; 2. Generate acceptancesuite and configure PhpBrowser or WebDriver through tests/acceptance.suite.yml; 3. Write a Cest test class to simulate user behavior, such as accessing pages, filling in forms, clicking buttons and verifying results; 4. Run vendor/bin/codeceptrunaccep after starting the local server and Selenium (such as using WebDriver)

How to deploy a Yii application to a serverHow to deploy a Yii application to a serverAug 11, 2025 am 11:24 AM

DisabledebugmodeandsetYII_DEBUGtofalse,2.UploadcodeviaGit,SFTP,orCI/CDandruncomposerinstall--no-devonserver,3.InstallPHP7.4 withrequiredextensionsandconfigureApacheorNginxwithproperrewriterules,4.Setfilepermissionswithchmod755andchownforruntimeandweb

Yii: most common errorsYii: most common errorsAug 11, 2025 am 09:23 AM

Common errors when using the Yii framework include configuration errors, database connection errors, and verification errors. 1. Configuration error: Check the config/web.php or config/main.php file to ensure there are no spelling errors or path errors. 2. Database connection error: Make sure the db.php file is configured correctly and the database server is running normally. 3. Verification error: Check the model rules to ensure that the verification settings meet application requirements.

How to write complex database queries with Query Builder in YiiHow to write complex database queries with Query Builder in YiiAug 08, 2025 pm 01:41 PM

Yii's QueryBuilder supports efficient and secure construction of complex database queries by providing smooth interfaces. 1. Use leftJoin() and other methods to achieve multi-table association and avoid field ambiguity; 2. Nesting subqueries in WHERE or SELECT to handle dynamic filtering or aggregated data; 3. Dynamically add where, orderBy and other conditions according to runtime conditions; 4. Combine groupBy() and having() to filter the aggregate results; 5. Use union() to merge query results compatible with multiple structures; 6. Securely insert SQL expressions through yii\db\Expression; 7. Combine limit and offset to realize pagination and use c

See all articles

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools