Play Yii Framework with TurnKey Linux


Yii Framework is the base of ZurmoCRM. To learn more about customization, I begin to learn Yii. It is very different from CodeIgniter. More object-oriented features and style.

I test the Yii Framework in TurnKey Linux and add CRUD to the demo application.

Setup TurnKey Yii Framework

Download TurnKey Linux: Yii Framework and import to your virtual machine software.

During first-time setup, it will ask password for Linux root, MySQL root, and Gii (a web-based code generator); email for Yii Framework admin. Remember to download security updates before begin.

Login to Demo Site

Now, test if it works by using the virtual machine IP address as url to launch “My Web Application”. You may login with user name demo and password demo. It is a simple web site with a contact form.

You cannot login with admin / admin as displayed. After reading Yii Autentication With Mysql, I found the existing site users are saved in /var/www/yiiframework/protected/components/UserIdentity.php. According to the source code, you should login with admin / turnkey.

$users=array(
        // username => password
        'demo'=>'demo',
        'admin'=>'turnkey',
);

Use Gii to Generate Code

Click on Gii in the home page of “My Web Application” or browse http://virtual machine IP address/index.php?r=gii directly. It provides following generators.

  1. Controller Generator
  2. Crud Generator
  3. Form Generator
  4. Model Generator
  5. Module Generator

Generate a Sample Database Table

The default database connection in TurnKey Linux Yii Framework is MySQL which is different from Creating Your First Yii Application on Yii’s Official website. When you edit /var/www/yiiframework/protected/config/main.php, you will notice sqlite has been comment out and mysql stay uncomment.

/*
'db'=>array(
        'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
*/
'db'=>array(
        'connectionString' => 'mysql:host=localhost;dbname=yiiframework',
        'emulatePrepare' => true,
        'username' => 'yiiframework',
        'password' => '2dfe6c98b2c849d52528694f8ddda0af',
        'charset' => 'utf8',
),

Therefore, you will need to use /var/www/yiiframework/protected/data/schema.mysql.sql and phpMyAdmin to generate the table. Here is the SQL statements:

CREATE TABLE tbl_user (
    id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(128) NOT NULL,
    password VARCHAR(128) NOT NULL,
    email VARCHAR(128) NOT NULL
);

INSERT INTO tbl_user (username, password, email) VALUES ('test1', 'pass1', 'test1@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test2', 'pass2', 'test2@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test3', 'pass3', 'test3@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test4', 'pass4', 'test4@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test5', 'pass5', 'test5@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test6', 'pass6', 'test6@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test7', 'pass7', 'test7@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test8', 'pass8', 'test8@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test9', 'pass9', 'test9@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test10', 'pass10', 'test10@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test11', 'pass11', 'test11@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test12', 'pass12', 'test12@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test13', 'pass13', 'test13@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test14', 'pass14', 'test14@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test15', 'pass15', 'test15@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test16', 'pass16', 'test16@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test17', 'pass17', 'test17@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test18', 'pass18', 'test18@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test19', 'pass19', 'test19@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test20', 'pass20', 'test20@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test21', 'pass21', 'test21@example.com');

Generate Model

Click on “Model Generator”. Use tbl_user as “Table Name” and User as “Model Class”. Press Preview button then Generate to confirm.

It generates /var/www/yiiframework/protected/models/User.php which will help to generate appropriate views and controllers.

Generate CRUD

Click on “Crud Generator”. Use User as “Model Class” and user as “Controller ID”. Press Preview button then Generate to confirm.

It generates following controller and view class files in /var/www/yiiframework/protected/.

  1. controllers/UserController.php
  2. views/user/_form.php
  3. views/user/_search.php
  4. views/user/_view.php
  5. views/user/admin.php
  6. views/user/create.php
  7. views/user/index.php
  8. views/user/update.php
  9. views/user/view.php

If you want to change the view layout, modify templates in /usr/local/share/yiiframework/framework/gii/generators/crud/templates/default.

Test your CRUD

Click on “try it now” in “Crud Generator” page or browse http://virtual machine IP address/index.php?r=user, you will see a detail list page.

Now, if you login as demo / demo, you may create new user but get an unauthorized message for manage or delete. You need to login as admin / turnkey to have full access. It is all controller by public method accessRules() in  /var/www/yiiframework/protected/controllers/UserController.php.

public function accessRules()
{
        return array(
                array('allow',  // allow all users to perform 'index' and 'view' actions
                        'actions'=>array('index','view'),
                        'users'=>array('*'),
                ),
                array('allow', // allow authenticated user to perform 'create' and 'update' actions
                        'actions'=>array('create','update'),
                        'users'=>array('@'),
                ),
                array('allow', // allow admin user to perform 'admin' and 'delete' actions
                        'actions'=>array('admin','delete'),
                        'users'=>array('admin'),
                ),
                array('deny',  // deny all users
                        'users'=>array('*'),
                ),
        );
}

Change ‘users’ from ‘admin’ to ‘admin’,’demo’ in line 14  will grant manage and delete operation to demo.

Thoughts

I am surprised with these generators. They are helpful to build a database prototype. It should help first-time developer to learn from using them.

Reference

  1. EllisLab: CodeIgniter
  2. Linux.org
  3. MySQL
  4. phpMyAdmin
  5. TurnKey Linux
  6. TurnKey Linux: Yii Framework
  7. Wiki: Create, read, update and delete
  8. Wiki: IP address
  9. Wiki: Uniform resource locator
  10. Wiki: Virtual machine
  11. Yii Framework
  12. Yii Framework: Forum: Yii Autentication With Mysql
  13. Yii Framework: Tutorials: Creating Your First Yii Application
  14. Yii Framework: Tutorials: Automatic Code Generation
  15. ZurmoCRM

2 thoughts on “Play Yii Framework with TurnKey Linux

  1. Looks like you are using TurnKey a fair bit in your ‘how to’ blogs. Thanks for helping us to help more people to help themselves! 🙂

    Perhaps we could cross post some of your blog posts on our blog? Drop me an email if you want… jeremy AT turnkeylinux.org

    Liked by 1 person

    • Dear Jeremy,

      I am glad I could help! TurnKey Linux has done a wonderful job to simplified installation and setup.

      All TurnKey Linux related post may be found with tag TurnKey Linux.

      Have a nice day!

      Best regards,

      Amigo

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s