[SOLVED] urlManager formatting

Hi there,

I’m not sure how to do this, it’s a general url rewriting question as well as a urlManager question.

I have a URL that is basically a user friendly search url, so for example:


/buy/houses-in-stoke+on+trent

What I need is this to redirect to:


/property/search?category=houses&location=stoke+on+trent

I thought the following would work, but I just get a 404 error:


'urlManager'=>array(

    ...

    'rules'=>array(

        'buy/<category:\d+>\-<in>\-<location:\d+>'=>'property/search',

        ...

    ),

),

I’m not 100% sure how to get the ‘-in-’ out of the url and pull the other two words as GET variables. I managed the same thing before, converting:


houses/aberdeen/555-1A+Something+Street

To:


property/view?id=555

Using:


'urlManager'=>array(

    ...

    'rules'=>array(

        '<category>/<city>/<id:\d+>\-<address>/*'=>'property/view',

        ...

    ),

),

And the above code for the property view page works perfectly. I thought I was using the same principles as the above rule to get the category and location from the search url but it’s just not working!

I also think there may be an issue with the ‘+’ symbol in the location string if I do get this working?

Any hints?

Thanks,

Stu

[font="arial, verdana, tahoma, sans-serif"][size="2"]<category:\d+> and <location:\d+> expects category and location to be decimal numbers. <category:\w+> and <location:\w+> should be used instead.[/size][/font]

Of course! Thanks!

Just one more issue, is there a regexp to ignore any + symbols in the url, for example i now have as my urlManager rule:


'<for:\w+>/<category:\w+>\-<in>\-<location:\w+>' => 'property/search',

Which works great, it converts:


/rent/houses-in-aberdeen

to:


/property/search?for=rent&category=houses&location=aberdeen

However it’s not allowing me to put the + symbol in the url, and if there’s a space in the town name or category name, the url will contain the + symbol, eg:


/rent/houses-in-stoke+on+trent

And this throws a 404 not found error. Does anyone know best way around this?

Thanks,

Stu

It seems that + signs are replaced by spaces automatically before routing, so you need to check for spaces in the pattern.

A regexp-guru may come up with a better solution but this will do the job:




<location:[a-zA-Z][a-zA-Z\040]*[a-zA-Z]>



Thanks a lot for that, it’s absolutely perfect! I’ve never really got on with regexp, I think I need to take some time out to learn it a little better!

Cheers,

Stu