Roshan Book

My Tech Notebook

Tag Archives: yii

Eclipse based Yii IDE for Ubuntu

Starting YII development in ubuntu – Part 1

1. Download yii folder

2. Go to var/www directory in terminal

3. TO create a new project follow:

first you need to get into your webroot by

cd /var/www <– in my case

create a folder that you want your demo to be install in my case I create a folder named ‘blog’ under /var/www/yii/

then set permission to 777 by: sudo chmod 777

then type in the following command

php yii/framework/yiic.php webapp /var/www/yii/blog

if your php program has not been installed, the system will give you an install command then do it and come back to do the same.

then you can access your demo by http://localhost/yii/blog <— in my case

4. To enable gii

go to protected>config>main.php and uncomment gii module

5. To access gii generator go to http://localhost/myproj/demo/index.php?r=gii/default/login

6. To convert url mapping into path format go to config>main.php and uncomment

‘urlManager’=>array(
‘urlFormat’=>’path’,
‘rules’=>array(
‘<controller:\w+>/<id:\d+>’=>'<controller>/view’,
‘<controller:\w+>/<action:\w+>/<id:\d+>’=>'<controller>/<action>’,
‘<controller:\w+>/<action:\w+>’=>'<controller>/<action>’,
),
),

 

7. Passing variable data from controller to view

In Controller

public $message = ‘Hello World rom controller’;

public function actionIndex()
{
$this->render(‘index’,array(‘content’=>$this->message));
}

Now variable content is available to modified

In View

<?php echo $content ?>

Alternatively we could do

<?php echo $this->message   ?>

8. CRUD generation

a) Config database

b) Go to protected>>config>>main.php

c) Uncomment db componenent

d) Give the username , password, and db name

e) Comment out sqlite configuration which lie just above the mysql configuration

 

9. GO to gii

10. Select model generator

11. Enter table details and generate the model

12. We now have Message.php under model directory

13. Now click on CRUD generator

13. Enter Model name

14. Click generate

15. You can try it out by goign to baseurl/modelname

16. Access database content via these codes in controller

public $message = ”;

public function actionIndex()
{ $message=Message::model()->findByPK(3);
$this->message=$message->content;
$this->render(‘index’,array(‘content’=>$this->message));
}

Starting YII development in ubuntu – Part 2

Source: http://yiitutorials.net/easy/easy-url-rewriting-with-yii

Nothing is worse than an ugly URL. Well, Hitler was quite bad, but so are ugly URL’s. Not only are they ugly for your users, but as clever as google is, they are ugly for it too. How is google supposed to know that “products/index.php?cat=3&id=46″ is an iPod nano? Today we will be looking at making the sexiest thing in the world, nice URL’s. Jessica Alba isn’t bad either.

william

Yii makes it insanely easy to rewrite URL’s to whatever you want. All of it is done in our config/main.php. Let’s open that bad boy up and see what’s inside. If you scroll down to around line line 34ish you will see the urlManager. If we uncomment this we get much nicer URL’s straight away. Insead of having ugly variables in the URL we will now have pretty good looking controller/action URL’s. So if we are using our product controller and looking at the browser action the URL would look like “index.php/product/browse”. Not to bad, but what about the index.php? We can take that out using our .htaccess file. Nobody understands. htaccess files, so instead of trying to write your own, just download this one that my friend William gave me. Click here to download it.

Just chuck that in your root directory and you never have to see and index.php again. Now, what about custom URL’s? We want the URL to look like it is an apple iPod. Let’s pretend that the URL now looks like “products/browse/prod/46″. Like I said early, we can make it look like whatever we want! Open up your main/config.php again, and scrolldown to somewhere around line 37ish. You will see:

	'<controller:\w+>/<id:\d+>'=>'<controller>/view',
	'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
	'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

That is Yii’s URL rewriting at work. Now, above that we can add the line:

	'catalogue/apple/ipod'=>'products/browse/prod/46',

Thats it. Now when Yii follows the link “catalogue/apple/ipod” the controller/view variable/value it will open is “products/browse/prod/46″ but the URL will be sexy. Told you it was easy. Stay tuned for our advanced tutorial on dynamic URL rewriting coming one day soon.

 
Posted by admin   @   17 November 20101 comments

 
 
%d bloggers like this: