Inside Yii 2 - Part 1
In this post, I will be starting off with a gentle introduction to what I call Inside Yii2. Yii is one of the PHP frameworks out there and Yii2, which is a complete rewrite over the previous version (Yii 1.1) was released in 2014. I’m currently using this framework and I would like to document some of my experiences with it, and I do hope that it would be of benefit to others.
There is nothing in this post about comparing this framework with other PHP frameworks, since this is not the main focus of this post.
This post also assumes that you are familiar with PHP.
Shall we?
Before we proceed, we have some checklists (in form of requirements) to tick. The following are the requirements:
- PHP 5.6+ (You must have PHP 5.6 or above installed on your machine)
- Composer - PHP package manager (You should also install composer)
You can install composer in linux and Mac by following the instruction below:
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
On Windows, you’ll download and run Composer-Setup.exe.
Having done that, and with no devil in the details, you should see the output like the image below, by running the command:
composer -version
Output:
Now we are good to go!
Yii2 project structure
In order to understand a typical Yii 2 application structure, we should first create a project.
More details about Yii 2 basic application can be found here.
First we need to install the composer asset plugin, composer will use this for managing bower and npm package dependencies (We shouldn’t care much about this for now, but we need it for the second command to be successful).
composer global require "fxp/composer-asset-plugin:^1.3.1"
composer create-project --prefer-dist yiisoft/yii2-app-basic inside-yii2
Now open the project directory in your favourite editor - I will use visual studio code here.
From the structure we can see directories like controllers, models and views,
this is because Yii makes use of the MVC design pattern.
“Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces on computers. It divides a given application into three interconnected parts in order to separate internal representations of information from the ways that information is presented to and accepted from the user. The MVC design pattern decouples these major components allowing for efficient code reuse and parallel development.” - Wikipedia
There is a detailed Yii 2 structure explanation here, which gives more information about the application structure.
Below is an extract from that page.
basic/ application base path
composer.json used by Composer, describes package information
config/ contains application and other configurations
console.php the console application configuration
web.php the Web application configuration
commands/ contains console command classes
controllers/ contains controller classes
models/ contains model classes
runtime/ contains files generated by Yii during runtime, such as logs and cache files
vendor/ contains the installed Composer packages, including the Yii framework itself
views/ contains view files
web/ application Web root, contains Web accessible files
assets/ contains published asset files (javascript and css) by Yii
index.php the entry (or bootstrap) script for the application
yii the Yii console command execution script
For every application, there is always an entry point to it from the outside world! In Yii 2, this entry point is web/index.php
.
Every request to the application is directed to this file.
The code in this file will bootstrap the application for every request. The following are what goes on in this file:
- the first two lines are for debug and development purposes
- the
autoload
file is required, which will make available all the dependencies in thevendor
directory - the dependencies that we required incomposer.json
- Yii bootstrap file is also included.
- the
config
file is loaded. - lastly we create an instance of the application and call the
run
method on it.
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
(new yii\web\Application($config))->run();
Controllers
At this point, one of the questions that comes to mind is where do we define the routes to our application? Interestingly, Yii handles that automatically! I will explain briefly how it does that.
Let’s say we will have a route like this http://localhost:8888/auth/user/login
for user login
. We need to structure our controller to match that.
And we need to do the following 3 simple things:
We must have a structure like
controllers/auth/
for our controllerWe must create a controller class
UserController
(which extends Yii base controller) in a fileUserController.php
Lastly, we must have a controller method
actionLogin
- in Yii every controllermethod/function
must be preceded byaction
namespace app\controllers\auth;
use yii\web\Controller;
class UserController extends Controller
{
/**
* Login.
*
* @return string
*/
public function actionLogin()
{
return $this->render('login');
}
}
The above is our controller with the login action.
Views
Inside the controller action, we have the line that renders the login view. In order to get this right, we also need to follow a structure for the view.
return $this->render('login');
Yii will look for a file named login.php
inside the views/auth/user
, I hope you can see the pattern
controller -
controllers/auth/UserController.php
view -
views/auth/user/login.php
Simple right? We just need to follow this convention going forward.
Also, the request lifecycle is well described here.
To run and see the application, we will make use of the in-built PHP web server, since it’s a quick way to serve the application for development.
php yii serve --port=8888
- You should see
Server started on http://localhost:8888/
on your console, visit the address in your browser and you will see the app homepage.
Hurray! You have just deployed your Inside Yii 2 app in dev
.
I have followed the getting started guide to prepare this post. We need this foundation here, so that in the next part we will have something to build upon.
In the next post, Inside-Yii2 part 2, we will look at models
, database interactions
, scenarios
e.t.c.
WATCH OUT!
You can get the application code on github - inside-yii2 repo, just checkout the getting started branch.