Yii is another powerful PHP Framework created by Qiang Xue and his core team of Alexander Makarov, Maurizio Domba, Carsten Brandt, Timur Ruziev, Klimov Paul, Antonio Ramirez, Wei Zhuo, Sebastián Thierer, and Jeffrey Winesett. Yii is also strongly supported by Larry Ullman, a well known and respected developer and author of countless books on programming. Yii lists several frameworks and technologies as inspiration in it’s development. Prado is listed as an inspiration for the component based, event driven, database connectivity abstraction, and modular concepts it is known for. Ruby on Rails is an influence as the Yii Object Relational Mapper is based on the active record design pattern of rails. jQuery is not only an inspiration, but baked into the Yii framework by default. Yii also makes use of Symfony for design inspiration in the plug in and filter architecture. Let’s install Yii 2 right now and check it out!
Yii 2 Installation Steps
Go install composer if you do not already have it installed. It works with any operating system so you’ll be covered no matter what you are using.
1. Add Github Credentials To Composer
Do yourself a favor and add your GitHub credentials to your composer installation to eliminate the annoying rate limits you will encounter using composer without credentials.
Follow the steps here https://getcomposer.org/doc/articles/troubleshooting.md#api-rate-limit-and-oauth-tokens to make that happen for yourself.
2. Configure Asset Plugin
Configure composer for the asset plugin which managing bower and npm package dependencies through Composer.
composer global require "fxp/composer-asset-plugin:1.0.0-beta3"
3. Create a new Yii 2 Project!
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
You’ll see something like this in your terminal:
C:wampwwwyii>composer create-project --prefer-dist yiisoft/yii2-app-basic basic Installing yiisoft/yii2-app-basic (2.0.0) - Installing yiisoft/yii2-app-basic (2.0.0) Loading from cache Created project in basic Loading composer repositories with package information Installing dependencies (including require-dev) - Installing yiisoft/yii2-composer (2.0.0) Downloading: 100% - Installing swiftmailer/swiftmailer (v5.3.0) Loading from cache - Installing bower-asset/jquery (2.1.1) Downloading: 100% - Installing bower-asset/yii2-pjax (v2.0.1) Downloading: 100% - Installing bower-asset/punycode (v1.3.2) Downloading: 100% - Installing bower-asset/jquery.inputmask (3.1.34) Downloading: 100% - Installing cebe/markdown (1.0.1) Downloading: 100% - Installing ezyang/htmlpurifier (v4.6.0) Downloading: 100% - Installing yiisoft/yii2 (2.0.0) Downloading: 100% - Installing yiisoft/yii2-swiftmailer (2.0.0) Downloading: 100% - Installing yiisoft/yii2-codeception (2.0.0) Downloading: 100% - Installing bower-asset/bootstrap (v3.2.0) Downloading: 100% - Installing yiisoft/yii2-bootstrap (2.0.0) Downloading: 100% - Installing yiisoft/yii2-debug (2.0.0) Downloading: 100% - Installing bower-asset/typeahead.js (v0.10.5) Downloading: 100% - Installing phpspec/php-diff (v1.0.2) Downloading: 100% - Installing yiisoft/yii2-gii (2.0.0) Downloading: 100% - Installing fzaninotto/faker (v1.4.0) Downloading: 100% - Installing yiisoft/yii2-faker (2.0.0) Downloading: 100% Writing lock file Generating autoload files chmod('runtime', 0777)...done. chmod('web/assets', 0777)...done. chmod('yii', 0755)...done. C:wampwwwyii>
Sweet! Believe it or not, you now have a fully functioning Yii Web Application built and ready to use. When you install Yii, you can choose to have it create a skeleton application and this is exactly what we just did. Let’s take a look at what we have.
Welcome To Yii
http://localhost/yii/basic/web/index.php
In this scenario, we created a folder named yii
in our root directory. This install was completed on Windows, so you we simply right clicked on the folder and chose use composer here which quickly opens a terminal window for us ready to accept composer commands. It is here where you typed the command from steps 3 and 4 above. This created a Yii Application in our newly created basic folder.
First up, you’ll notice that the example application is in a bootstrap style. Bootstrap v3.2.0 is baked right into the Yii repository so you get access to this right off the bat. Nice touch.
The top navbar is responsive and has a company logo or tagline option on the top left, as well as an additional about page, contact page, and login page. These are all functional by the way right upon install. Clicking the about link brings us to:
About
This is the About page. You may modify the following file to customize its content:
C:wampwwwyiibasicviewssiteabout.php
Clicking the contact links brings up a very nice contact form as you see here:
Even though this is the local development environment, go ahead and fill out all of the form fields, then submit the form. You will be greeted with this nice message.
Note that if you turn on the Yii debugger, you should be able to view the mail message on the mail panel of the debugger. Because the application is in development mode, the email is not sent but saved as a file under C:wampwwwyiibasicruntime/mail
. Please configure the useFileTransport
property of the mail
application component to be false to enable email sending.
Nice Touch! It looks like by default, Yii is configured to simply log email requests. This way, you can build out the application, and simply verify in the logging system that the mail functionality is working. When you are ready, it can be turned on for production.
Yii Debug Bar
This brings us to another really nice feature by way of the Yii Debug Bar. It provides a wealth of information including the version of Yii running, the PHP version in use, Status codes, the currently executing controller and method name, the number of log messages, response times, memory used, and asset bundles. Awesome stuff here friends! This debugger provides an absolute treasure trove of information. If you click the bar, look at some of the data you get:
Configuration
Application Configuration
Name | Value |
---|---|
Yii Version | 2.0.0 |
Application Name | My Application |
Environment | dev |
Debug Mode | Yes |
Installed Extensions
Name | Value |
---|---|
yiisoft/yii2-swiftmailer | 2.0.0.0 |
yiisoft/yii2-codeception | 2.0.0.0 |
yiisoft/yii2-bootstrap | 2.0.0.0 |
yiisoft/yii2-debug | 2.0.0.0 |
yiisoft/yii2-gii | 2.0.0.0 |
yiisoft/yii2-faker | 2.0.0.0 |
PHP Configuration
Name | Value |
---|---|
PHP Version | 5.5.12 |
Xdebug | Enabled |
APC | Disabled |
Memcache | Disabled |
Request
Routing
Name | Value |
---|---|
Route | ‘site/captcha’ |
Action | ‘yii\captcha\CaptchaAction::run()’ |
Parameters | [] |
$_GET
Name | Value |
---|---|
r | ‘site/captcha’ |
v | ‘54516b06e37f6’ |
$_POST
Empty.
$_FILES
Empty.
$_COOKIE
Name | Value |
---|---|
bbfa5b72 | |
_csrf | |
PHPSESSID | ‘i38h0u0p9l5jptni3563cilba4’ |
Request Body
Empty.
Request Headers
Name | Value |
---|---|
host | ‘localhost’ |
user-agent | ‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0’ |
accept | ‘image/png,image/*;q=0.8,*/*;q=0.5’ |
accept-language | ‘en-US,en;q=0.5’ |
accept-encoding | ‘gzip, deflate’ |
referer | ‘http://localhost/yii/basic/web/index.php?r=site%2Fcontact’ |
cookie | |
connection | ‘keep-alive’ |
Response Headers
Name | Value |
---|---|
X-Powered-By | ‘PHP/5.5.12’ |
Pragma | ‘public’ |
Expires | ‘0’ |
Cache-Control | ‘must-revalidate, post-check=0, pre-check=0’ |
Content-Transfer-Encoding | ‘binary’ |
Content-Type | ‘image/png’ |
$_SESSION
Name | Value |
---|---|
__flash | [] |
__captcha/site/captcha | ‘sormkf’ |
__captcha/site/captchacount | 1 |
Flashes
Empty.
$_SERVER
Name | Value |
---|---|
HTTP_HOST | ‘localhost’ |
HTTP_USER_AGENT | ‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0’ |
HTTP_ACCEPT | ‘image/png,image/*;q=0.8,*/*;q=0.5’ |
HTTP_ACCEPT_LANGUAGE | ‘en-US,en;q=0.5’ |
HTTP_ACCEPT_ENCODING | ‘gzip, deflate’ |
HTTP_REFERER | ‘http://localhost/yii/basic/web/index.php?r=site%2Fcontact’ |
HTTP_COOKIE | ” |
HTTP_CONNECTION | ‘keep-alive’ |
PATH | |
SystemRoot | ‘C:\Windows’ |
COMSPEC | ‘C:\Windows\system32\cmd.exe’ |
PATHEXT | ‘.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC’ |
WINDIR | ‘C:\Windows’ |
SERVER_SIGNATURE | ‘<address>Apache/2.4.9 (Win32) PHP/5.5.12 Server at localhost Port 80</address> ‘ |
SERVER_SOFTWARE | ‘Apache/2.4.9 (Win32) PHP/5.5.12’ |
SERVER_NAME | ‘localhost’ |
SERVER_ADDR | ‘127.0.0.1’ |
SERVER_PORT | ’80’ |
REMOTE_ADDR | ‘127.0.0.1’ |
DOCUMENT_ROOT | ‘C:/wamp/www/’ |
REQUEST_SCHEME | ‘http’ |
CONTEXT_PREFIX | ” |
CONTEXT_DOCUMENT_ROOT | ‘C:/wamp/www/’ |
SERVER_ADMIN | ‘admin@example.com’ |
SCRIPT_FILENAME | ‘C:/wamp/www/yii/basic/web/index.php’ |
REMOTE_PORT | ‘60785’ |
GATEWAY_INTERFACE | ‘CGI/1.1’ |
SERVER_PROTOCOL | ‘HTTP/1.1’ |
REQUEST_METHOD | ‘GET’ |
QUERY_STRING | ‘r=site%2Fcaptcha&v=54516b06e37f6’ |
REQUEST_URI | ‘/yii/basic/web/index.php?r=site%2Fcaptcha&v=54516b06e37f6’ |
SCRIPT_NAME | ‘/yii/basic/web/index.php’ |
PHP_SELF | ‘/yii/basic/web/index.php’ |
REQUEST_TIME_FLOAT | 1414621959.204 |
REQUEST_TIME | 1414621959 |
# | Time | Level | Category | Message |
---|---|---|---|---|
1 | 23:32:39.251 | trace | yiibaseApplication::bootstrap | Bootstrap with yiilogDispatcher |
2 | 23:32:39.251 | trace | yiibaseModule::getModule | Loading module: debug |
3 | 23:32:39.277 | trace | yiibaseApplication::bootstrap | Bootstrap with yiidebugModule::bootstrap() |
4 | 23:32:39.286 | trace | yiibaseModule::getModule | Loading module: gii |
5 | 23:32:39.291 | trace | yiibaseApplication::bootstrap | Bootstrap with yiigiiModule::bootstrap() |
6 | 23:32:39.296 | trace | yiiwebUrlManager::parseRequest | Pretty URL not enabled. Using default URL parsing logic. |
7 | 23:32:39.298 | trace | yiiwebApplication::handleRequest | Route requested: ‘site/captcha’ |
8 | 23:32:39.308 | trace | yiibaseController::runAction | Route to run: site/captcha |
9 | 23:32:39.326 | trace | yiibaseAction::runWithParams | Running action: yiicaptchaCaptchaAction::run() |
10 | 23:32:39.336 | info | yiiwebSession::open | Session started |
appassetsAppAsset |
|
sourcePath | C:/wamp/www/yii/basic/web |
---|---|
basePath | C:/wamp/www/yii/basic/web |
baseUrl | /yii/basic/web |
css |
|
depends |
|
yiiwebYiiAsset |
|
sourcePath | C:wampwwwyiibasicvendoryiisoftyii2/assets |
basePath | C:wampwwwyiibasicwebassetse822b307 |
baseUrl | /yii/basic/web/assets/e822b307 |
js |
|
depends |
|
yiiwebJqueryAsset |
|
sourcePath | C:wampwwwyiibasicvendorbower/jquery/dist |
basePath | C:wampwwwyiibasicwebassets8489d356 |
baseUrl | /yii/basic/web/assets/8489d356 |
js |
|
yiibootstrapBootstrapAsset |
|
sourcePath | C:wampwwwyiibasicvendorbower/bootstrap/dist |
basePath | C:wampwwwyiibasicwebassetsd6e3c75b |
baseUrl | /yii/basic/web/assets/d6e3c75b |
css |
|
yiibootstrapBootstrapPluginAsset |
|
sourcePath | C:wampwwwyiibasicvendorbower/bootstrap/dist |
basePath | C:wampwwwyiibasicwebassetsd6e3c75b |
baseUrl | /yii/basic/web/assets/d6e3c75b |
js |
|
depends |
|
Very slick stuff indeed. The base install also points you to three very helpful links.
Yii Docs
Yii Forums
Yii Extensions
In addition to this, there is a fully functioning login page with support for an admin and demo account. All pages have a slick breadcrumb structure as well to boot.
Conclusion
In this episode we took a look at installing the Yii Framework. It’s a snap, all you have to do is run a couple of composer commands and the entire framework is installed with a base application for you to kick the tires on. We’ll be looking at Yii more in the episodes to come so do stay tuned!