[YII] Pretty Url


Deskripsi
     Secara pengaksesan standart YII punya format url yang mengerikan / horrible, so jadi bagaimana agar kita membuat URL yang cukup cantik dan simple pada YII. simple just follow my step

Howto
1. First access URL in YII like this :
    localhost/myYii/index.php?r=controller/action

2. i want to change like this :
    localhost/myYii/index.php/controller/action

3. Open config.php at path : webroot/YourYiiApp/protected/config/config.php
4. Find and Uncomment this string code :

              // uncomment the following to enable URLs in path-format
             '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>',
),
 ),

5. Now your url like this
    localhost/myYii/index.php/controller/action

6. it's still bad i want to remove index.php, haha ok to remove index.php we need .htaccess

7. create .htaccess at this path : webroot/myYii/[your htaccess here]

8. copy paste code below to your htaccess
 
    Options +FollowSymLinks
    IndexIgnore */*
    <IfModule mod_rewrite.c>
    RewriteEngine on

    # if a directory or a file exists, use it directly

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # otherwise forward it to index.php

   RewriteRule . index.php
   </IfModule>
 
9. now your URL like this :
     localhost/myYii/controller/action

10. How about the parameter ? , i want to passing the value from url to my action in controller , simply follow this :
      localhost/myYii/controller/action?myvar1=1&myvar2=2

11 In method  you can do this
   
     in your action on controller

     public function actionXXZZ($myvar1,$myvar2)
     {
           echo $myvar1;
     }

*beware !! the name of variable in URL must same in action parameter it's very different with Cakephp.

ok all done , hope's this help

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih