logo
Ask your Symfony questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Allow forward slashes in variables

I have an application allowing to search by tag, the corresponding routing.yml part looks like this:


blog_search_tag:
url: /blog/tags/:tags
param: { module: blog, action: search }


This works until a tag contains a forward slash ("/").

How do I allow forward slashes in variables and pass them to the action?

[UPDATE]

My case doesn't seem to be clear. I don't want to separate several tags by "/". :tags will always contain one tag only, I however want this tag to be able to contain a "/".

This question has been answered.

sn | 12/15/10 at 3:22am Edit

Previous versions of this question: 12/15/10 at 9:06am

The experts have suggested, on average, a prize of $4 for this question.

(6) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    12/15/10
    3:34am
    Alex Zgorzhelsky says:

    I guess you could use another segment separator instead of forward slash for you route
    Underscore, for example


    blog_search_tag:
    url: /blog/tags/:tags
    param: { module: blog, action: search }
    options: { segment_separators: [_, ., -] }

    Previous versions of this answer: 12/15/10 at 3:34am

  • avatar
    Last edited:
    12/15/10
    3:39am
    Gordon Franke says:

    @alex right the segment_seperator

    @sn be warned search engines don't like to many levels and each forward slash stand for a deeper website level, the better option is to make a getTagForUrl method in that you replace some characters.

    and than use:

    options: { model: Tag, type: object, method: getForTagRouting, suffix: '' }


    or something similar

    Previous versions of this answer: 12/15/10 at 3:39am

  • avatar
    Last edited:
    12/16/10
    8:27am
    Christian Schaefer says:

    You can also define each variable on its own.
    You might want to read this as well.

  • avatar
    Last edited:
    12/15/10
    9:00am
    Jérôme Tamarelle says:

    You could simply create many routes redirecting to the same controller.


    blog_search_tag1:
    url: /blog/tags/:tag1
    param: { module: blog, action: search }

    blog_search_tag2:
    url: /blog/tags/:tag1/:tag2
    param: { module: blog, action: search }

    blog_search_tag3:
    url: /blog/tags/:tag1/:tag2/:tag3
    param: { module: blog, action: search }


    This way you don't have to split your "tags" parameter in the controller. You can access them using

    $this->get RequestParameters()

  • avatar
    Last edited:
    12/16/10
    8:27am
    Bernhard Schussek says:

    You have to change the pattern for the variable. By default, variables don't match slashes. Furthermore you have to define a custom route that decodes the slashes (which are urlencoded by default).

      blog_search_tag:
    url: /blog/tags/:tags
    class: SlashRoute
    param: { module: blog, action: search }
    requirements: { tags: .+ }


    SlashRoute.php
    class SlashRoute extends sfRequestRoute
    {
    /**
    * Utility function to unencode urlencoded slashes within a URL.
    *
    * @param string $url The original URL
    * @param boolean $allowSlashes Whether to allow unencoded slashes
    * @return string The cleaned URL
    */
    static public function decodeSlashes($url, $allowSlashes = true)
    {
    if ($allowSlashes)
    {
    $url = str_replace('%2F','/', $url);
    }

    return $url;
    }

    /**
    * @see sfRequestRoute
    */
    public function generate($params, $context = array(), $absolute = false,
    $allowSlashes = true)
    {
    return self::decodeSlashes(parent::generate($params, $context, $absolute), $allowSlashes);
    }
    }


    You can use decodeSlashes() in other routes (f.i. SlashObjectRoute) as well.

  • avatar
    Last edited:
    12/15/10
    4:03am
    sn says:

    In this case, _, ., - wouldn't be allowed as var content. Is there a way to allow "any" character as variable content?

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.