Url Manager & Search Filter

Hi people,

I’m trying to modify the urlManager rules for my filter bar search. Currently my url manager looks as follow:




'urlManager' => array(

                    'urlFormat' => 'path',

                    'showScriptName' => false,

                    'urlSuffix' => '/',

                    'rules' => array(

                        '<page>/<q:.*?>/<cat:.*?>' => 'site/index',

                        '<page>/<q:.*?>/<province:.*?>' => 'site/index',

                        '<page>/<q:.*?>' => 'site/index',

                        '<page>' => 'site/index',

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

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

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

                    ),

                ),




When filtering by “cat”(first rule) all its working but when I try to filter by province (2nd rule) I’m getting the value of parameter “cat” from the first rule. Does anybody know how to fix this? Thanks.

Hi florin

both of two rules have the same pattern (in SEO mode)

‘<page>/<q:.?>/<cat:.?>’ => ‘site/index’,

‘<page>/<q:.?>/<province:.?>’ => ‘site/index’,

so if the url is

some/some2/somethree then site/index action called with page=some, q=some2, cat=somethree and the first pattern match for every "xxx/yyy/zzz"

So you have to find a way to seperate the patterns with discrete format

Hi KonApaz,

Thanks for your fast reply and for your observation, now I figured out, however it’s first time when I met this kind of problem. Basically i have a filter sidebar for search, when the user press on the filter link a new param will be appended to the url through a custom function. Before posting here i tried to use a rule like “<page>/<q:.?>/” this works but the url will looks like: http://localhost/mywebsite/pagename/search-string/cat/category-name-here/ or http://localhost/mywebsite/pagename/search-string/province/province-name-here/ do you have any idea how could i get rid of the “cat” or “province” from url?

Thanks,

Florin

could you post the entire code of filter search sidebar ?

Here is my view code for generating the categories filters:




// we use filter by category only if the cat param is not set

if (empty($_GET['cat'])) {

                echo '<ul>';

                foreach ($counts as $k => $count) {

                    $name = Categories::model()->findByPk($k)->name . ' (' . $count . ')';

                    $slug = Categories::model()->findByPk($k)->slug;

                    $url = add_url_param(array('cat' => $slug . '-' . $k));

                    echo '<li>';

                    echo CHtml::link($name, $url);

                    echo '</li>';

                }

                echo '</ul>';

            }


// we use filter by province only if the province param is not set

if (empty($_GET['province'])) {

                echo '<ul>';

                foreach ($counts as $k => $count) {

                    $name = Province::model()->findByPk($k)->name . ' (' . $count . ')';

                    $slug = strtolower(Province::model()->findByPk($k)->name);

                    $url = add_url_param(array('province'=>$slug .'-'. $k));

                    echo '<li>';

                    echo CHtml::link($name, $url);

                    echo '</li>';

                }

                echo '</ul>';

            }




Then in my helper functions file i have a simple function add_url_param for creating the url:




// add params to url

function add_url_param($addparams = array()) {

    $route = Yii::app()->urlManager->parseUrl(Yii::app()->getRequest());

    $params = $_GET;


    //we remove the pagination param

    if (isset($params['page'])) {

        unset($params['page']);

    }

   

    foreach($addparams as $k => $val){

        $params[$k] = $val;

    }

    $url = Yii::app()->createUrl('/' . $route, $params);

    return $url;

}



If I understood the way you want to do that, then

$url = Yii::app()->createUrl(’/’ . $route, $params);

[b]echo $url;

die();[/b]

return $url;

if the echo display "cat" or "province" then check values both of $route and $params

If I am you, I will try to do only with urlManager rules

In any other case (like using method add_url_param) may the entire project become too complicated

Thanks for your reply, that won’t solve my problem, I’ll keep looking arround and post here if I find any hint.