Seven reasons to choose the yii framework:
(Recommended tutorial: yii framework)
1. Simple installation
For web developers, time is money. No one wants to waste their time on tedious installation and configuration.
Composer will handle the installation process. Well, if you want a description of the installation process, Sitepoint recently published an article about this, which you can read below. I prefer to stick with the basic app template even though I have a separate front-end and back-end component on my site. Instead, I chose to use a module for the backend portion of my site. (Yii modules are the best representation of mini-applications, used to embed into your main application.)
Note: Many of the following examples use simple directory references. The directory structure of the template.
2. Use modern technology
Yii is a pure OOP framework that makes full use of some of PHP’s more Advanced features include lazy static binding, SPL classes and interfaces, and anonymous functions.
All classes are called namespaces and allow you to take advantage of their PSR-4 compliant autoloader. In other words, including the HTML auxiliary classes in Yii, it is equally simple:
use?yii\helpers\Html;
Yii can also define aliases to help simplify your namespace. In the above example, the use statement will load a class definition, and the default path in the directory is /vendor/yiisoft/yii2/helpers. This alias is defined in the BaseYii class on line 79:
public?static?$aliases?=?['@yii'?=>?__DIR__];
The framework itself is installed using Composer, as is its extension. Publishing an extension is even as simple as creating a composer.json, hosting your code on Github, and listing the extension on Packagist. Yii can also define aliases to help simplify your namespace. In the above example, the use statement will load a class definition, and the default path in the directory is /vendor/yiisoft/yii2/helpers. This alias is defined in line 79 of the BaseYii class:
3. High degree of scalability
Yii Just like a suit, it looks big but is actually easy to adjust to fit your needs. Virtually every component of the framework is extensible. A simple example is adding a unique body ID to your view. (If you want to know why you want to do this, you can check this article)
First, I will in my app\components Create a file called View.php and add the following content:
namespaceapp\components; ? classView?extendsyii\web\View?{ ? ????public$bodyId; ? ????/*?Yii?allows?you?to?add?magic?getter?methods?by?prefacing?method?names?with?"get"?*/ ? ????publicfunction?getBodyIdAttribute()?{ ????????return($this->bodyId?!='')??'id="'?.?$this->bodyId?.'"'?:?''; ????} ? }
Then, in my main layout file (app \views\layouts\main.php), I want to add the following content to the body tag of HTML:
Finally, I want to add the following content to the main configuration file so that Yii can use the extended View class instead of the default one:
return[ ????//?... ????'components'=>?[ ????????//?... ????????'view'=>?[ ????????????'class'=>?'app\components\View' ????????]?? ????] ];
4 , Encourage testing
#Yii and Codeception are closely connected. Codeception is an amazing PHP testing framework that helps simplify the process of creating widgets, functional tests, and acceptance tests for your applications. Because every application you write is an automated test, right?
The Codeception extension will make configuring your application easier during testing. Simply provide the /tests/_config.php file to configure your test program. For example:
return[ ????'components'=>?[ ????????'mail'=>?[ ????????????'useFileTransport'=>?true, ????????], ????????'urlManager'=>?[ ????????????'showScriptName'=>?true, ????????], ????????'db'=>?[ ????????????????'dsn'=>?'mysql:host=localhost;dbname=mysqldb_test', ????????], ????], ];
使用該配置,將會(huì)發(fā)生如下幾種情況:
1、?在功能測(cè)試和驗(yàn)收測(cè)試期間發(fā)送的任何一封郵件,將會(huì)被寫入一個(gè)文件中,而不是被發(fā)送。使用該配置,將會(huì)發(fā)生如下這種情況:
2、?在你測(cè)試中的URLs將會(huì)采取這種格式:?index.php/controller/action?而不是這種:/controller/action
3、?測(cè)試將會(huì)使用你的測(cè)試數(shù)據(jù)庫(kù),而不是你的產(chǎn)品數(shù)據(jù)庫(kù)。
存在Yii框架中的特殊模塊,同樣也存在Codeception內(nèi)。它TestGuy類中增加了幾個(gè)方法,協(xié)助你在功能測(cè)試中的活動(dòng)記錄(Yii中的ORM)。舉例來說,如果你想看看是否成功創(chuàng)建了一個(gè)用戶名為“testuser”的新user,你可以做以下幾點(diǎn):
$I->amOnPage('register'); $I->fillField('username','testuser'); $I->fillField('password','qwerty'); $I->click('Register'); $I->seeRecord('app\models\User',array('name'=>?'testuser'));
5、簡(jiǎn)化安全性
安全性是任何一個(gè)web應(yīng)用的重要組成部分,幸運(yùn)的是,Yii有一些很棒的功能可以幫你解決這方面的煩惱。
Yii帶有一個(gè)安全應(yīng)用組件,其中公開的幾個(gè)方法可以幫助創(chuàng)建一個(gè)更安全的應(yīng)用。其中一些比較有用的方法如下:
·generatePasswordHash:?從一個(gè)密碼和隨機(jī)因子生成一個(gè)安全的單向散列函數(shù)。這種方法為你編譯了一個(gè)隨機(jī)因子,然后通過PHP?crypt函數(shù)功能提供的字符串創(chuàng)建了一個(gè)單項(xiàng)散列函數(shù)。
·validatePassword:對(duì)于generatePasswordHash,這是一個(gè)伴侶功能,并且允許你檢查用戶提供的密碼是否與你存儲(chǔ)的散列函數(shù)相匹配。
·generateRandomKey:允許你創(chuàng)建一個(gè)任意長(zhǎng)度的隨機(jī)字符串。
Yii會(huì)對(duì)所有不安全的HTTP請(qǐng)求方法(PUT,POST,DELETE)進(jìn)行自動(dòng)檢查,當(dāng)你使用ActiveForm::begin()方法創(chuàng)建開放表單標(biāo)簽時(shí),它會(huì)生成并輸出一個(gè)token。通過編輯你的主配置文件可以禁止此功能,方法如下:
return[ ????'components'=>?[ ????????'request'=>?[ ????????????'enableCsrfValidation'=>?false, ????????] ];
為了防止XSS,Yii提供了一個(gè)叫HtmlPurifier的輔助類。這個(gè)類有一個(gè)名為process的靜態(tài)方法,并且會(huì)使用popular?filter?library過濾出同名的輸出庫(kù)。
Yii還包括備用類,用來進(jìn)行用戶身份驗(yàn)證和授權(quán)。授權(quán)分為兩種類型:ACF(訪問控制過濾器)和RBAC(基于角色的訪問控制)。
這兩種授權(quán)方法,較簡(jiǎn)單的要數(shù)ACF了,它是通過在你的控制器中添加以下行為方法來實(shí)現(xiàn)的:
useyii\filters\AccessControl; ? classDefaultController?extendsController?{ ????//?... ????publicfunction?behaviors()?{ ????????return[ ????????????//?... ????????????'class'=>?AccessControl::className(), ????????????'only'=>?['create','login','view'], ????????????????'rules'=>?[ ????????????????[ ????????????????????'allow'=>?true, ????????????????????'actions'=>?['login','view'], ????????????????????'roles'=>?['?'] ????????????????], ????????????????[ ????????????????????'allow'=>?true, ????????????????????'actions'=>?['create'], ????????????????????'roles'=>?['@'] ????????????????] ????????????] ????????]; ????} ????//?... }
上面的代碼用于區(qū)分DefaultControllerto,允許guest用戶的訪問login?和view行為,而不是create?行為。(??是一個(gè)匿名用戶別名,@?指的是已認(rèn)證用戶)。
RBAC是指那些用戶可以在整個(gè)應(yīng)用中執(zhí)行特定操作行為的更有效的方法。包括為用戶創(chuàng)建角色,定義app權(quán)限,然后使這些權(quán)限試用于相應(yīng)的角色。如果你想創(chuàng)建一個(gè)Moderator的角色,并允許分配給該角色的所有用戶批準(zhǔn)文章。
你也可以使用RBAC定義角色,它允許你在特定條件下,授權(quán)訪問應(yīng)用的某些方面的自定義規(guī)則。例如,你可以創(chuàng)建一個(gè)規(guī)則,即允許用戶編輯自己的文章,而不是那些其他人創(chuàng)建的。
6、縮短開發(fā)時(shí)間
大多數(shù)項(xiàng)目都會(huì)涉及一定的重復(fù)任務(wù),沒有人愿意浪費(fèi)時(shí)間。而Yii提供的一些工具可以幫助你減少在這些任務(wù)中所花費(fèi)的時(shí)間,將更多的時(shí)間用于定制讓客戶滿意的應(yīng)用。
在這些工具中,其中有一個(gè)名為“Gii”的工具最為強(qiáng)大。Gii是一個(gè)基于web的基架代碼工具,可以讓你快速創(chuàng)建代碼模板:
·模型
·控制器
·形式
·模塊
·擴(kuò)展
·CRUD控制器行為和視圖
Gii是高度可配置的。你可以將其設(shè)置為僅在特定的環(huán)境下加載。只需簡(jiǎn)單編輯下你的web配置文件即可,方法如下:
if?(YII_ENV_DEV)?{ ????//?... ????$config['modules']['gii']?=?[ ????????'class'?=>?'yii\gii\Module', ????????'allowedIPs'?=>?['127.0.0.1',?'::1'] ????] }
這樣就可以確保黨Yii的環(huán)境設(shè)置為開發(fā)的時(shí)候,Gii僅支持加載,并且它只通過本地主機(jī)訪問時(shí)才會(huì)加載。
現(xiàn)在,讓我們來看下模型生成器吧:
表名使用的是一個(gè)預(yù)輸入控件,來試圖猜測(cè)哪個(gè)表格與你的模型相關(guān)聯(lián),并且所有領(lǐng)域都有一個(gè)翻轉(zhuǎn)工具,提示你如何填寫出來。在用Gii生成它之前,你可以預(yù)覽代碼,并且所有代碼模板是完全可以自定義的。
還有幾個(gè)命令行工具可以幫你為你的自動(dòng)化測(cè)試創(chuàng)建數(shù)據(jù)庫(kù)遷移,信息翻譯(I18N:國(guó)際化)和數(shù)據(jù)庫(kù)fixtures?代碼模板。例如,你可以使用如下命令創(chuàng)建一個(gè)新的數(shù)據(jù)庫(kù)遷移文件:
yii?migrate/create?create_user_table
這將會(huì)在?{appdir}/migrations上創(chuàng)建一個(gè)新的遷移模板,看起來像這樣的:
<?php use yii\db\Schema; class m140924_153425_create_user_table extends \yii\db\Migration { public function up() { } public function down() { echo "m140924_153425_create_user_table cannot be reverted.\n"; return false; } }
所以我們可以說,我想添加在該表中再添加幾列。我只想添加以下內(nèi)容到up 方法中:
public function up() { $this->createTable('user',?[ ????????'id'?=>?Schema::TYPE_PK, ????????'username'?=>?Schema::TYPE_STRING?.?'?NOT?NULL', ????????'password_hash'?=>?Schema::?TYPE_STRING?.?'?NOT?NULL' ????],?null); }
然后,保證我可以反向遷移,下面我將添加down?方法:
public?function?down() { $this->dropTable('user'); }
創(chuàng)建該表將會(huì)簡(jiǎn)單包括一個(gè)在運(yùn)行在命令行的命令:
./yii?migrate
然后移除該表:
./yii?migrate/down
7、容易調(diào)整為最佳性能
大家都知道,一個(gè)網(wǎng)站很慢的話會(huì)很容易讓用戶產(chǎn)生不滿,所以Yii提供了幾種工具來幫助你從應(yīng)用中“擠”出更多的速度。
所有Yii的緩存組件都是從yii/caching/Cache擴(kuò)展來的,你可以選擇任何一種,你想同時(shí)使用一個(gè)通用API擴(kuò)展的緩存系統(tǒng)。你甚至可以注冊(cè)多個(gè)高速緩存組件。Yii目前支持?jǐn)?shù)據(jù)庫(kù)和文件緩存,APC,Memcache,?Redis,?WinCache,?XCache和Zend?數(shù)據(jù)緩存。
默認(rèn)情況下,如果你正在使用Active?Record,然后Yii會(huì)運(yùn)行一個(gè)額外的查詢,來確定表參與生成模型的架構(gòu)。你可以通過編輯注配置文件設(shè)置應(yīng)用程序,從而緩存這些架構(gòu):
return?[ ????//?... ????'components'?=>?[ ????????//?... ????????'db'?=>?[ ????????????//?... ????????????'enableSchemaCache'?=>?true, ????????????'schemaCacheDuration'?=>?3600, ????????????'schemaCache'?=>?'cache', ????????], ????????'cache'?=>?[ ????????????'class'?=>?'yii\caching\FileCache', ????????], ????], ];
最后,Yii有一個(gè)命令行工具,使前端資源極簡(jiǎn)化更容易。只需運(yùn)行以下命令來生成配置模板:
./yii?asset/template?config.php
然后,編輯該配置來指定工具,達(dá)到你想要的簡(jiǎn)化效果(如關(guān)閉編譯器,?YUI?Compressor,或UglifyJS)。生成的配置模板,如下所示:
<?php return [ 'jsCompressor' =>?'java?-jar?compiler.jar?--js?{from}?--js_output_file?{to}', ????????'cssCompressor'?=>?'java?-jar?yuicompressor.jar?--type?css?{from}?-o?{to}', ????????'bundles'?=>?[ ????????????//?'yii\web\YiiAsset', ????????????//?'yii\web\JqueryAsset', ????????], ????????'targets'?=>?[ ????????????'app\config\AllAsset'?=>?[ ????????????????'basePath'?=>?'path/to/web', ????????????????'baseUrl'?=>?'', ????????????????'js'?=>?'js/all-{hash}.js', ????????????????'css'?=>?'css/all-{hash}.css', ????????????], ????????], ????????'assetManager'?=>?[ ????????????'basePath'?=>?__DIR__, ????????????'baseUrl'?=>?'', ????????], ????];
接著,運(yùn)行此控制臺(tái)命令來實(shí)現(xiàn)壓縮:
'components'?=>?[ ????//?... ????'assetManager'?=>?[ ????????'bundles'?=>?require?'/app/assets_compressed.php' ????] ]
注意:你必須要手動(dòng)下載和安裝這些外部工具。
結(jié)論:
像任何一個(gè)好的框架一樣,Yii能夠幫助你快速創(chuàng)建流行的web應(yīng)用,并確保它們可以做的很好。通過做許多繁瑣的事情,它幫你你創(chuàng)建安全的和可測(cè)試的網(wǎng)站。你可以輕松的使用它提供的大部分功能,或者你也可以修改其中任何一個(gè)來適應(yīng)你的需求。真心建議你在你的下一個(gè)web項(xiàng)目中考慮一下它!
The above is the detailed content of What are the reasons for choosing the yii framework?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Yii framework middleware: providing multiple data storage support for applications Introduction Middleware (middleware) is an important concept in the Yii framework, which provides multiple data storage support for applications. Middleware acts like a filter, inserting custom code between an application's requests and responses. Through middleware, we can process, verify, filter requests, and then pass the processed results to the next middleware or final handler. Middleware in the Yii framework is very easy to use

In recent years, with the rapid development of the game industry, more and more players have begun to look for game strategies to help them pass the game. Therefore, creating a game guide website can make it easier for players to obtain game guides, and at the same time, it can also provide players with a better gaming experience. When creating such a website, we can use the Yii framework for development. The Yii framework is a web application development framework based on the PHP programming language. It has the characteristics of high efficiency, security, and strong scalability, and can help us create a game guide more quickly and efficiently.

Yii framework middleware: Add logging and debugging capabilities to applications [Introduction] When developing web applications, we usually need to add some additional features to improve the performance and stability of the application. The Yii framework provides the concept of middleware that enables us to perform some additional tasks before and after the application handles the request. This article will introduce how to use the middleware function of the Yii framework to implement logging and debugging functions. [What is middleware] Middleware refers to the processing of requests and responses before and after the application processes the request.

With the rapid development of web applications, modern web development has become an important skill. Many frameworks and tools are available for developing efficient web applications, among which the Yii framework is a very popular framework. Yii is a high-performance, component-based PHP framework that uses the latest design patterns and technologies, provides powerful tools and components, and is ideal for building complex web applications. In this article, we will discuss how to use Yii framework to build web applications. Install Yii framework first,

Steps to implement web page caching and page chunking using the Yii framework Introduction: During the web development process, in order to improve the performance and user experience of the website, it is often necessary to cache and chunk the page. The Yii framework provides powerful caching and layout functions, which can help developers quickly implement web page caching and page chunking. This article will introduce how to use the Yii framework to implement web page caching and page chunking. 1. Turn on web page caching. In the Yii framework, web page caching can be turned on through the configuration file. Open the main configuration file co

In the Yii framework, controllers play an important role in processing requests. In addition to handling regular page requests, controllers can also be used to handle Ajax requests. This article will introduce how to handle Ajax requests in the Yii framework and provide code examples. In the Yii framework, processing Ajax requests can be carried out through the following steps: The first step is to create a controller (Controller) class. You can inherit the basic controller class yiiwebCo provided by the Yii framework

In modern web application development, debugging tools are indispensable. They help developers find and solve various problems with their applications. As a popular web application framework, the Yii framework naturally provides some debugging tools. This article will focus on the debugging tools in the Yii framework and discuss how they help us analyze and debug applications. GiiGii is a code generator for the Yii framework. It can automatically generate code for Yii applications, such as models, controllers, views, etc. Using Gii,

Encrypting and decrypting sensitive data using Yii framework middleware Introduction: In modern Internet applications, privacy and data security are very important issues. To ensure that users' sensitive data is not accessible to unauthorized visitors, we need to encrypt this data. The Yii framework provides us with a simple and effective way to implement the functions of encrypting and decrypting sensitive data. In this article, we’ll cover how to achieve this using the Yii framework’s middleware. Introduction to Yii framework Yii framework is a high-performance PHP framework.
