Home » Php » php – how to setup url friendly in yii framework automatically

php – how to setup url friendly in yii framework automatically

Posted by: admin July 12, 2020 Leave a comment

Questions:

I just learning yii framework and read this tutorial
about yii how to setup url

but I have no idea, suppose i have 10 controllers, should I define one by one controllers in the config file ?
is there a better way to setup url friendly like www.yoursite.com/yourController/yourAction/yourID for all controller ?

I think codeigniter did that automatically … how about yii ?

How to&Answers:

In /protected/config/main.php add..

    'urlManager'=>array(
        'urlFormat'=>'path',
        'showScriptName' => false,      
        ),
    ),

In your web root an .htaccess..

Options +FollowSymLinks
IndexIgnore */*
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

Answer:

There are automatical URL generation in Yii too. For example just write in your template such URL without manual route:

<?php echo CHtml::link('topic title',array('topic/view','id'=>$topic->id,'var'=>'123')); ?>

And rendered URL will be as follow:

/topic/view/id/1/var/123

Then in our action actionView() method we use these parameters:

...
$id=$_GET['id'];
$var=$_GET['var'];
...