Yii Framework Forum: Url Manager & Search Filter - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Url Manager & Search Filter Rate Topic: -----

#1 User is offline   florin p 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 25-September 11

Posted 11 December 2012 - 07:40 AM

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.
0

#2 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,268
  • Joined: 21-February 11
  • Location:Greece

Posted 11 December 2012 - 07:57 AM

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
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#3 User is offline   florin p 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 25-September 11

Posted 11 December 2012 - 08:13 AM

View PostKonApaz, on 11 December 2012 - 07:57 AM, said:

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"



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/myw.../search-string/cat/category-name-here/ or http://localhost/myw.../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
0

#4 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,268
  • Joined: 21-February 11
  • Location:Greece

Posted 11 December 2012 - 09:17 AM

View Postflorin p, on 11 December 2012 - 08:13 AM, said:

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/myw.../search-string/cat/category-name-here/ or http://localhost/myw.../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 ?
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#5 User is offline   florin p 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 25-September 11

Posted 11 December 2012 - 09:51 AM

View PostKonApaz, on 11 December 2012 - 09:17 AM, said:

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;
}

0

#6 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,268
  • Joined: 21-February 11
  • Location:Greece

Posted 11 December 2012 - 11:01 AM

View Postflorin p, on 11 December 2012 - 09:51 AM, said:

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);
echo $url;
die();

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
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#7 User is offline   florin p 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 25-September 11

Posted 11 December 2012 - 11:53 AM

View PostKonApaz, on 11 December 2012 - 11:01 AM, said:

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

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

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 coblicated



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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users