Adam Stacey
Adam Stacey

Reputation: 2831

Symfony2 Routing with Folders (containing multiple forward slashes)

I am looking to setup some routes in Symfony2, but I am struggling to setup a dynamic route for folders.

I am trying to setup a route that accepts the following: /department/sub-department/sub-sub-department/product-url.html

From that route all I need is the product-url and the rest is more for SEO. The problem I have is that a route may have many department levels in the URL, so I need to ignore everything before the product-url.

It seems like the "/" is the problem here, so is there a way to escape the slashes.

If I don't use any of the departments in the routing I can use this:

product:
    pattern:  /{url}.html
    defaults: { _controller: CompanyBundle:System:pageRequest }

So, I basically need something like this:

product:
    pattern:  /{department}/{url}.html
    defaults: { _controller: CompanyBundle:System:pageRequest }

Where the {department} can be one or more departments with forward slashes in.

Is that possible at all?

Upvotes: 0

Views: 489

Answers (2)

VlastV
VlastV

Reputation: 86

product:
    pattern: /{url}.html
    defaults: { _controller: CompanyBundle:System:pageRequest, department: ~ }

product_department:
    pattern: /{department}/{url}.html
    defaults: { _controller: CompanyBundle:System:pageRequest }
    requirements:
        department: '[\w\d\/\-]+'

Upvotes: 1

kgilden
kgilden

Reputation: 10356

There's a nice article about it in the cookbook:

You must explicitly allow / to be part of your parameter by specifying a more permissive regex pattern.

In your case the route definition would have to be

product:
    pattern: /{department}/{url}.html
    defaults: { _controller: CompanyBundle:System:pageRequest }
    requirements:
        department: ".+"

Upvotes: 1

Related Questions