There are many reasons to select Yii2 framework. However, among all of them, Here, I am going to share good three reasons to choose it. Let me start.
1. Easy Installation :-
For Web developers, Time is very important and no one wants to spend too much time in any installation and configuration process.
Installation is handled using composer. I suggest to favor using the basic app template, even when a site has a separate front-end and back end component. Instead, I optimize to use a module for the back end portion of my site. Yii modules are the best as a mini-applications which reside inside your main application.
2. Highly Extensible :-
Yii is like a suit that looks great off of the rack, but is also very easy to tailor to fit your needs. Every component within the framework is extensible. A simple example is the addition of a unique body ID to your views.
First, I would create a file in my app\components directory with the name View.php, and add the following:
namespace app\components;
class View extends yii\web\View {
public $bodyId;
/* Yii allows you to add magic getter methods by prefacing method names with "get" */
public function getBodyIdAttribute() {
return ($this->bodyId != '') ? 'id="' . $this->bodyId . '"' : '';
}
}
Then, in my main file (app\views\layouts\main.php), I would add the following to the body tag of my HTML:
<body <?=$this->BodyIdAttribute?>>
Finally, I would add below code to my main configuration file to let Yii known to use my extended view class instead of its own default:
return [
// ...
'components' => [
// ...
'view' => [
'class' => 'app\components\View'
]
]
];
3. Shorten Development Time :-
Yii gives us a few tools to help you spend less time in a repetitive task, and more time customizing your application to suit your clients’ needs. One of the powerful of these tools is called "Gii". Gii is a tool, which allows you to quickly create code templates for :-
Models
Controllers
Forms
Modules
Extensions
CRUD controller actions and views
Gii is highly configurable. You can set it to only load in certain environments. Simply edit your web configuration file as follows:
if (YII_ENV_DEV) {
// ...
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1', '::1']
]
}
This ensures that Gii will only load when the Yii environment variable is set to development, and that it will only load when accessed via local host.
Now let’s take a look at the model generator:
The table name uses a type ahead widget to try to guess which table your model is associated with, and all fields have a rollover tool tip to remind you how to fill them out. Furthermore, you can see a preview of code before generated it.
There are several command-line tools available to help create code templates for database migrations, message translations & database fixtures for your automated tests. For instance, you can create a new database migration file with this command:
yii migrate/create create_user_table
This will create a new migration template in {appdir}/migrations that look something like this:
<?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;
}
}
So now, I would simply add the following to the up method:
public function up()
{
$this->createTable('user', [
'id' => Schema::TYPE_PK,
'username' => Schema::TYPE_STRING . ' NOT NULL',
'password_hash' => Schema:: TYPE_STRING . ' NOT NULL'
], null);
}
After that make sure I can reverse the migration, I would edit the down method :
public function down()
{
$this->dropTable('user');
}
Creating the table would simply involve running a command on the command line:
./yii migrate
and to remove the table:
./yii migrate/down
1. Easy Installation :-
For Web developers, Time is very important and no one wants to spend too much time in any installation and configuration process.
Installation is handled using composer. I suggest to favor using the basic app template, even when a site has a separate front-end and back end component. Instead, I optimize to use a module for the back end portion of my site. Yii modules are the best as a mini-applications which reside inside your main application.
2. Highly Extensible :-
Yii is like a suit that looks great off of the rack, but is also very easy to tailor to fit your needs. Every component within the framework is extensible. A simple example is the addition of a unique body ID to your views.
First, I would create a file in my app\components directory with the name View.php, and add the following:
namespace app\components;
class View extends yii\web\View {
public $bodyId;
/* Yii allows you to add magic getter methods by prefacing method names with "get" */
public function getBodyIdAttribute() {
return ($this->bodyId != '') ? 'id="' . $this->bodyId . '"' : '';
}
}
Then, in my main file (app\views\layouts\main.php), I would add the following to the body tag of my HTML:
<body <?=$this->BodyIdAttribute?>>
Finally, I would add below code to my main configuration file to let Yii known to use my extended view class instead of its own default:
return [
// ...
'components' => [
// ...
'view' => [
'class' => 'app\components\View'
]
]
];
3. Shorten Development Time :-
Yii gives us a few tools to help you spend less time in a repetitive task, and more time customizing your application to suit your clients’ needs. One of the powerful of these tools is called "Gii". Gii is a tool, which allows you to quickly create code templates for :-
Models
Controllers
Forms
Modules
Extensions
CRUD controller actions and views
Gii is highly configurable. You can set it to only load in certain environments. Simply edit your web configuration file as follows:
if (YII_ENV_DEV) {
// ...
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1', '::1']
]
}
This ensures that Gii will only load when the Yii environment variable is set to development, and that it will only load when accessed via local host.
Now let’s take a look at the model generator:
The table name uses a type ahead widget to try to guess which table your model is associated with, and all fields have a rollover tool tip to remind you how to fill them out. Furthermore, you can see a preview of code before generated it.
There are several command-line tools available to help create code templates for database migrations, message translations & database fixtures for your automated tests. For instance, you can create a new database migration file with this command:
yii migrate/create create_user_table
This will create a new migration template in {appdir}/migrations that look something like this:
<?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;
}
}
So now, I would simply add the following to the up method:
public function up()
{
$this->createTable('user', [
'id' => Schema::TYPE_PK,
'username' => Schema::TYPE_STRING . ' NOT NULL',
'password_hash' => Schema:: TYPE_STRING . ' NOT NULL'
], null);
}
After that make sure I can reverse the migration, I would edit the down method :
public function down()
{
$this->dropTable('user');
}
Creating the table would simply involve running a command on the command line:
./yii migrate
and to remove the table:
./yii migrate/down
0 comments:
Post a Comment