开发者

Many levels of categories in symfony route rule

开发者 https://www.devze.com 2023-03-12 10:04 出处:网络
I want to create a symfony route rule where can have one o many levels, but just es important the last one. For better explanation, here is a ¨example¨:

I want to create a symfony route rule where can have one o many levels, but just es important the last one. For better explanation, here is a ¨example¨:

home_category:
  url:   /:sf_culture/开发者_StackOverflow:category1/:category2/:category3/:category4/:slug
  class: sfCategoryRoute
  param: { module: category, action: index }
  requirements:
    sf_culture: (?:es|en)

:category[n] are dinamics, can come one o many, and the last is the important to me. Any idea?

Thanks a lot.


I think something like this might do the trick:

home_category_1:
  url:   /:sf_culture/:category1/:slug
  param: { module: category, action: index }
home_category_2:
  url:   /:sf_culture/:category1/:category2/:slug
  param: { module: category, action: index }
home_category_3:
  url:   /:sf_culture/:category1/:category2/:category3/:slug
  param: { module: category, action: index }
home_category_4:
  url:   /:sf_culture/:category1/:category2/:category3/:category4/:slug
  param: { module: category, action: index }

... same module/action matched to each possible URL pattern. If the URL needs to be very many levels deep, I'd probably think of turning the middle part into a single variable, but maybe someone else has a more elegant solution.


My solution was extending sfRequestRoute, and overwrite the matchesUrl method, like that:

public function matchesUrl($url, $context = array())
{
    $url_tmp = explode('/', substr($url, 1));
    $url = (count($url_tmp)>1) ? '/'.$url_tmp[0].'/'.end($url_tmp) : $url ;

    if (false === $parameters = parent::matchesUrl($url, $context))
    {
        return false;
    }
....

so, the rule /:category catch /category1/category2 and /category1 and category1/category2/category3, ect.

be careful with that, because this rule also catch all routes in your aplication, you need apply extra validation inside the method before return the parameters.

Thanks for you reply anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号