Nerds who love the symfony-project
23 Apr
In general, you will always run into issues trying to use the ‘/’ character as part of a parameter in your URL in web development. Recently I’ve run into issues trying to pass a parameter in my URL that has the forwardslash character. For example - trying to pass a URL within a URL:
mymodule/myaction/url=http://google.com
In the case above, Apache will return a 404 as it cannot find that URL (it things that http://google.com is part of the URL).
This is a known issue that can be resolved in two ways:
1) urlencode() + ApacheEncodedSlashes
You could use urlencode() to escape your http:// slashes, and then change you’re ApacheEncodedSlashes directive to allow this (further reading). However this does require a Apache change.
2) The quick fix is to simply tell Apache that it is a parameter, not part of the URL, by changing the / to a ?
mymodule/myaction?url=http://google.com
This works fine. However, in Symfony you can’t get this to work if you are trying to use the link_to_remote Ajax Helper function.
The following code:
$url = "http://google.com"; echo link_to_remote($topic->getName(), array( 'update' => array('success' => 'topic_detail', 'failure' => 'topic_detail_err'), 'url' => "mymodule/myaction?t="$url, 'script' => 'true', 'loading' => visual_effect('appear', 'loading_animation'), 'complete' => visual_effect('appear', 'topic_detail') ));
Will produce this URL:
....mymodule/myaction/url=http://google.com
Notice that Symfony converts the ? to a /. This is because we are using link_to_remote. link_to_remote goes through the routing layer that converts all ? to / characters. If we were using url_for, this would not be an issue.
So how do we overcome this problem if we are using link_to_remote? Well it’s simple - use both the Symfony url_for() helper function as well as the PHP http_build_query function. The http_build_query function encodes the URL for us (so ‘/’ get converted to %2F, alongside other characters).
So the end result looks like this:
$url= url_for("mymodlue/myaction")."?".http_build_query(Array("t"=>$topic->getName())); echo link_to_remote($topic->getName(), array( 'update' => array('success' => 'topic_detail', 'failure' => 'topic_detail_err'), 'url' => $url, 'script' => 'true', 'loading' => visual_effect('appear', 'loading_animation'), 'complete' => visual_effect('appear', 'topic_detail') ));
This forces link_to_remote to use the ? instead of replacing it with a /. Hope this helps!