0

我想在 Yii 框架中创建友好的 url。

一个例子:

mysitename.com/country/city/travelling-type

或者

mysitename.com/country/city/travelling-type/price
mysitename.com/city/price

等等

问题是它混淆了参数。所以我尝试用下面的代码解决它,但还不够好:

'urlManager' => array(
        'urlFormat' => 'path',
        'appendParams' => true,
        'showScriptName' => false,
        'useStrictParsing' => FALSE,
        'rules' => array(
            '<country:\w+>/<city:\w+>/<travelling_type:\w+>/<accommodation:\w+>/<caftering:\w+>/<price_from:\d+/<price_to:\d+>' => 'Travels/list'
        ),
    ),
4

1 回答 1

2

Its not really easy to do that since Yii doesn't know the difference between those two URLs:

mysitename.com/{city}/{price}
mysitename.com/{city}/{accommodation}

What you can do though is change the route to this system:

'<param1:\w+>/<param2:\w+>/<param3:\w+>/<param4:\w+>/<param5:\w+>/<param6:\d+/<param7:\d+>' => 'Travels/list'

Your action would have to identify which query parameter represents which parameter for your search query. In some instances in your system, this should be easy enough. For example, there's a limited number of countries, there's probably a limited number of traveling types, etc.

i.e.

In case of

mysitename.com/{city}/{accommodation}

your system would query "param1" (= city) against a list of countries, cities, traveling types, accomodation types and catering types to determine that param1 is indeed a city. It would then proceed with param2 to determine that it defines the accomodation.

Prices are easier to identify because they are different data types (in case of a traveling search engine integer is probably good enough), so you can easily identify prices by simply checking if a price is an integer (or similar). To differentiate price_from and price_to I suggest you assume that if there's only one price, it's price_from; if there's two prices, the first is price_from, the second is price_to. If you want to only set price_to, you set price_from to 0.

I hope that covers what you want to do. It can be quite complicated to set up, but that's the price you have to pay for a URL route like that. :)

于 2013-05-03T08:26:16.853 回答