Thursday, November 14, 2013

Yii: Creating Demo Blog 1 (XAMPP)


--
Yii: Creating Demo Blog 1
This tutorial is based on the official documentation at http://www.yiiframework.com/doc/blog/
Contents
1) PREPARATION
2) LINKING TO DATABASE
3) SCAFFOLDING
4) CONFIGURE THE UserIdentity SCRIPT
5) TEST LOGIN
DOWNLOAD

1) PREPARATION

1.1) Create virtual hosts site1.local and site2.local. Point these hosts to c:/projects/site1 and c:/projects/site2/ respectively.
1.2) Extract Yii Framework contents from Yii archive file into c:\projects\masterframework.
1.3) Extract Yii Demo Blog contents into c:\projects\site1
1.4) Register PHP and Yii as Window Environment Variables.
1.5) Run yiic to create webapp into c:\projects\sites2
Or download the startup zip file, https://drive.google.com/file/d/0B86b-ALn-1MGZXJuMF9uZWdad1U/edit?usp=sharing to start immediately and proceed to next step.

2) LINKING TO DATABASE

2.1) Refer {webroot}/protected/config/main.php line no. 51
By default, Yii is set to connect to the sqlite, i.e testdrive.db
2.2) Edit line no. 51 so that the file name become ../data/blog.db.
2.3) Copy the blog.db file from site1 to site2.

3) SCAFFOLDING

3.1) Edit {webroot}/protected/config/main.php to enable gii
Uncomment line no. 23 and 30 by deleting the “/*” and “*/” characters)
Set password, e.g ‘password’.
Enter the password that you have set in the Step 3.1 above.
3.3) Gii Main Page.
We will be working with Model Generator and CRUD Generator.
3.4) Model Generator
3.4.1) Click the link Model Generator.
3.4.2) Enter as follows:
Table Prefix: tbl
Table Name: tbl_user
Model Class: User
3.4.3) Click Preview.
3.4.4) Click Generate.
3.4.5) Repeat the same steps for :
tbl_post
tbl_comment
tbl_tag
tbl_lookup
3.4.6) You can check that the above steps are successful by inspecting the files in {webroot}/protected/models/
3.5) CRUD Generator
3.5.1) Click the link CRUD Generator.
3.5.2) Enter “Post” in the Model Class.
3.5.3) Click Preview.
3.5.4) Click Generate.
3.5.5) Repeat the same steps for Comment.
3.5.6) Check {webroot}/protected/controllers
3.5.7) Check {webroot}/protected/views
3.6) Test

4) CONFIGURE THE UserIdentity SCRIPT

4.1) Open the file {webroot}/protected/components/UserIdentity.php and modify as follows:
<?php
class UserIdentity extends CUserIdentity
{
    private $_id;

    public function authenticate()
    {
        $username=strtolower($this->username);
        $user=User::model()->find('LOWER(username)=?',array($username));
        if($user===null)
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if(!$user->validatePassword($this->password))
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $this->_id=$user->id;
            $this->username=$user->username;
            $this->errorCode=self::ERROR_NONE;
        }
        return $this->errorCode==self::ERROR_NONE;
    }

    public function getId()
    {
        return $this->_id;
    }
}
4.2) Add the following codes to {webroot}/protected/models/User.php
public function validatePassword($password)
    {
        return CPasswordHelper::verifyPassword($password,$this->password);
    }

    public function hashPassword($password)
    {
        return CPasswordHelper::hashPassword($password);
    }

5) TEST LOGIN

5.2) Login successful.
Login is successful when the you see the Login label turned into Logout label.

DOWNLOAD



No comments:

Post a Comment

Labels