Jevgeni Smirnov
Jevgeni Smirnov

Reputation: 3797

Symfony2. Problems with custom URLs. Urlencoding

I have following route in my bundle

/{category}

And I have category with name

Category/Brand

If url is something like this:

domain.com/Company/Brand

then I get error 500 about / symbol.

But if in twig I do

company.name|url_encode()

then I get

Company%2F%Brand

(Code might be wrong, dont remember right now)

But nevertheless Symfony tells me that there is no route matching

And gives me 404.

How can I solve this problem?

Upvotes: 0

Views: 2262

Answers (3)

Jevgeni Smirnov
Jevgeni Smirnov

Reputation: 3797

Ok!

I found a solution here.


    #PUBLICATION URL
    ###_publication:
        pattern:  /{username}/{category}/{publicationid}
        defaults: { _controller: ###:Default:publication }
        requirements:
          _method:  GET
          category: .+
          username: .+ 
          publicationid: \d+                               
    #CATEGORY - should be at very end, to match all other URLS
    ###_category:
        pattern:  /{category}
        defaults: { _controller: ###:Default:category }
        requirements:
          _method:  GET 
          category: .+


Upvotes: 0

Carlos Granados
Carlos Granados

Reputation: 11351

Are all your category names like that one or only some of them?

If all of them follow this structure, you could change the route to:

/{company}/{brand}

And change the corresponding controller to accept two variables instead of one. Later you can concatenate them or do whatever you need with them

If only some of them have this structure, you could try to replace the directory separator with some character combination in the controller which creates the link and then reverse this replacement in the controller for this route. For example, in the controller for the template where the link is shown you could

$nameEncoded = str_replace ('/','%%%%',$companyName);

pass this variable to the template and use it to generate the link and then in the receiving controller do:

$nameDecoded = str_replace ('%%%%','/',$companyName);

Upvotes: 1

unairoldan
unairoldan

Reputation: 2863

If your route is /{category} and the URL that you type is domain.com/Company/Brand, the error is normal.

You have to config your route in routing.yml like this:

Company/{category}

Upvotes: 0

Related Questions