How to use url-friendly?

Today when I read the articles in Yii forum, I found this nice website: http://www.stay.com, in fact that, i pay more attention with her url-friendly.

for example, under her home page, the urls of the citys are as: http://www.stay.com/london, that is to say, there is not controller name before the city’s name, I am so curority with this, and then i use Yii blog to test how to do it.

[size="5"]1. Set the urlManager of main.php[/size]

protected/config/main.php




'urlManager'=>array(

    'class'=>'CUrlManager',

    'urlFormat'=>'path',

    //'urlSuffix'=>'.html',//Please note that, this value can NOT be used, i will descript the reason below.

    'showScriptName'=>false,

    'caseSensitive'=>false,//it makes route case-insensitive.

    'rules'=>array(

        'posts/<tag:.*?>'=>'post/index',

        '<controller:\w+>'=>'<controller>',

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

        '<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',

        '/'=>'post',

        '<titile:.*?>'=>'post/view',

    ),

),



Ok, now there is a problem, in the rules, [color="#FF0000"]the orders of the values can NOT change![/color] (at least when i testing), because the order will take effect to other url-request!

[size="5"]2. Create a glocal controller[/size]

protected/components/MyController.php




<?php

class MyController extends Controller

{

    public function init()

    {

        //That is why we can NOT set the 'urlSuffix'=>'.html' above in the route

        $arrUrl = explode('/', Yii::app()->request->url);

        $title = urldecode($arrUrl[count($arrUrl)-1]);

        

        //As you can know, the code below will make slow to every request.

        $criteria = new CDbCriteria;

        $criteria->condition = "title = '".$title."'";

        $criteria->limit = 1;

        $model = Post::model()->find($criteria);

        if (!empty($model))

        {

            $_GET['id'] = $model->id;

        }

    }

}



[size="5"]3. Modify PostController.php,let it to extend MyController[/size]

protected/controllers/PostController.php




<?php


class PostController extends MyController



[size="5"]4. Modify the posts model[/size]

protected/models/Post.php,find this function: getUrl(), change it to:




public function getUrl()

{

    return Yii::app()->createUrl($this->title);

}



Now, all the options have been finished, when visiting http://127.0.0.1/blog, then the website runs well, and then check the url of the posts, for example the Welcome!, its url now is http://127.0.0.1/blog/Welcome!, click it, runs well! Going testing other urls, no problems found.

We turn back to see the setting of rules.




    'rules'=>array(

        'posts/<tag:.*?>'=>'post/index',

        '<controller:\w+>'=>'<controller>',

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

        '<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',

        '/'=>'post',

        '<titile:.*?>'=>'post/view',

    ),



A. the value ‘<titile:.*?>’=>‘post/view’ has to be the last one, other wise, it will change other url request!

B. Without ‘/’=>‘post’, it will cause “The requested page does not exist.”, at the same time, this value has to been locate in the current place.

C. About ‘<module:\w+>/<controller:\w+>/<action:\w+>’=>’<module>/<controller>/<action>’, in fact that, I haven’t try to find whether or not there is <module:\+>, :P but after adding, the module can run without error (for example, the image of captcha can display or not in a module.)

But, is it really the way to use url-friendly? I think it is very strange in this method.

Please help me and give some ideas about how to use url-friendly, thank you. :)

Hi, friends, i need your help~

at first you have to have method in

post controller like this action

‘<titile:.*?>’=>‘post/view’,




........

public function actionView ($title) {

.....

}



do you have it?

also check Flexica CMS based on yii framework this can be help full for you.

yes. Sorry, i think you haven’t read the whole content of this article, the blog website runs well and didn’t get problem, but i want to know whether or not the method i used is right of url-friendly.

I wish to, but i can’t download the code in Flexica CMS because i can NOT register in it. If you have a copy, please email to me, thank you, :)

Anyway, thank you for your replying. ;)

write email into my pm.

thanks of R.K to email the files, and now i provide a url to down it, please see the details of

http://www.yiiframework.com/forum/index.php?/topic/10389-flexicacms-v0-3-released/page__st__20__gopid__60105#entry60105