March 12, 2015

Home » » » Build CRUD Application Demo in Yii Framework - Start Yii Step by Step

Build CRUD Application Demo in Yii Framework - Start Yii Step by Step

Posted by Unknown
3

Install Yii Framework in Ubuntu

Install with composer

Install PHP, MySQL, Apache2

$ sudo apt-get update
$ sudo apt-get apache2
$ sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
$ sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

Install Yii

$ curl -s http://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
$ composer global require "fxp/composer-asset-plugin:1.0.0"
$ cd /path/to/webroot/
$ composer create-project --prefer-dist yiisoft/yii2-app-basic basic

Test installation

$ sudo service apache2 restart
Access to : http://localhost/basic/web/index.php

Screenshot

Other install

Refer to : http://www.yiiframework.com/doc-2.0/guide-start-installation.html

Database setup

Create database

$ mysql -u root -p
mysql > create database yii2basic;
mysql > use yii2basic;
CREATE TABLE `country` (
  `code` CHAR(2) NOT NULL PRIMARY KEY,
  `name` CHAR(52) NOT NULL,
  `population` INT(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `country` VALUES ('AU','Australia',18886000);
INSERT INTO `country` VALUES ('BR','Brazil',170115000);
INSERT INTO `country` VALUES ('CA','Canada',1147000);
INSERT INTO `country` VALUES ('CN','China',1277558000);
INSERT INTO `country` VALUES ('DE','Germany',82164700);
INSERT INTO `country` VALUES ('FR','France',59225700);
INSERT INTO `country` VALUES ('GB','United Kingdom',59623400);
INSERT INTO `country` VALUES ('IN','India',1013662000);
INSERT INTO `country` VALUES ('RU','Russia',146934000);
INSERT INTO `country` VALUES ('US','United States',278357000);

Config

Open basic/config/db.php

<?php
return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=yii2basic',
    'username' => 'username',
    'password' => 'your_password',
    'charset' => 'utf8',
];

Test database setup

Create basic/models/Country.php
<?php

namespace app\models;

use yii\db\ActiveRecord;

class Country extends ActiveRecord
{
}
 
Create basic/controllers/CountryController.php

<?php

namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Country;
class CountryController extends Controller
{
    public function actionIndex()
    {
        $query = Country::find();
        $pagination = new Pagination([
            'defaultPageSize' => 5,
            'totalCount' => $query->count(),
        ]);
        $countries = $query->orderBy('name')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        return $this->render('index', [
            'countries' => $countries,
            'pagination' => $pagination,
        ]);
    }
}
 
Create view file

<?php
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1 >Countries</h1 >
<ul >
<?php foreach ($countries as $country): ?>
    <li >
        <?= Html::encode("{$country->name} ({$country->code})") ?>:
        <?= $country->population ?>
    </li >
<?php endforeach; ?>
</ul >
<?= LinkPager::widget(['pagination' => $pagination]) ?>

Access to : http://localhost/basic/web/index.php?r=country/index

Create simple CRUD demo

Yii framework provides Gii module to allow programmers to create one code base quickly and easily. Gii allows for Models, Controllers, Modules, the extensions and CRUD with a few simple operations.

Check Gii installed Access: http://localhost/basic/web/index.php?r=gii

Screenshot

Create CRUD using Gii

Gii code generated automatically by the user www-data by default, so it is necessary to assign the right permission( write and read) to basic directory for this user. 

To be able to CRUD genetic code automatically, to create a table for the entity you want to create. 

In this example, the country table created earlier. The properties in the table will be automatically gen in the sample code. Table should have a primary key.

Screenshots

After you click the Preview button, will see the message files will be created if click the Generate button. 

If there are files that were created before it can notify you overwrote or not, click the button diff will see the difference of the original file with the file will be created. 

Click the Generate button will automatically execute. 

Finish.

Screenshots

References: http://www.yiiframework.com/doc-2.0/guide-index.html






3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. When generating CRUD it is giving error on Model class


    'app\models\User' must extend from yii\db\BaseActiveRecord or its child class.

    ReplyDelete
  3. It is nice post and I found some interesting information on this blog, keep it up. Thanks for sharing. . .Dedicated Yii Developers in India

    ReplyDelete

Popular Posts

Labels

Archive

 

Blogroll

Recepies

Flickr Images

Copyright © 2014. Tutorials Blog - All Rights Reserved