Zend Framework 2 : Handling Trailing Slash “issue”
In fact, in web application, it’s better not to have two urls respond to the same thing. But we are a lazy programmer, right, RIGHT ? We don’t have a much time to remember the route where is end with “/” or not.
For example we have 2 url like this :
http://zf-tutorial/album/add – 200 – works fine
http://zf-tutorial/album/add/ – 404 – The requested URL could not be matched by routing.
To handle that, just add the empty “/” in the end of route segment :
'route' => '/[:controller[/[:action[/]]]]',
Or for “:slug” segment :
'route' => '/[:controller[/[:action[/[:slug[/]]]]]]',
I do not know if this is a best solution or not, but this is probably one of the easiest.
References :
1. http://zend-framework-community.634137.n4.nabble.com/Trailing-slash-td4655266.html
This is pretty bad 😦
It’s much much better to handle trailing slashes in the server’s rewrite rules.
Or at least have a separate route that matches when everything else failed and if it spots a trailing slash, it does a permanent redirect to a non-slash version of the url.
instead of saying “pretty bad”, maybe invest your time to give real other solution. post a real ‘code’ to do that, and i will update my blog post.
You can add the following code to your .htaccess file in public folder, right after the RewriteEngine On:
# The following removes trailing slash from URL
RewriteRule ^(.+)/$ /$1 [R=301,L]
Works great, thanks!
mantep Tutorial nya mas… lanjut terus
makasih
[…] Ссылка на оригинал […]
I have tried in place of controller route but it didnt work. Can you please help me on this? Please see the piece of code shared below.
‘options’ => array(
‘route’ => ‘/news’,
‘defaults’ => array(
‘controller’ => ‘News\Controller\Index’,
‘action’ => ‘index’,
),
Also, it would be great if you can share any of the simple sample project you have developed in zend framework 2.
your route is all wrong. try:
‘options’ => array(
‘route’ => ‘/news[/:controller][/:action]’,
‘constraints’ => array(
‘controller’ => ‘[a-zA-Z][a-zA-Z0-9-]*’,
‘action’ => ‘[a-zA-Z0-9-]+’,
),
‘defaults’ => array(
‘__NAMESPACE__’ => ‘News\Controller’,
‘controller’ => ‘Index’,
‘action’ => ‘index’,
),
),
You can add a optional trailling slash in each route:
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:controller[/:action[/:id]]][/]',
'constraints' => array(
'controller' => '[a-zA-Z]+',
'action' => '[a-zA-Z]+',
'id' => '[0-9]+',
),
'defaults' => array(
),
),
),
),
thanks!
This does allow trailing slashes which is great, but it also undesirably places trailing slashes at the end of URLs made with the `urlFor()` and similar helpers, which is bad.
instead of saying bad, you better contribute to the project to make it better 🙂
Or you could use
‘type’ => ‘Segment’,
‘options’ => array(
‘route’ => ‘/[:controller[/:action]][:trailingSlash]’,
‘constraints’ => array(
‘controller’ => ‘[a-zA-Z][a-zA-Z0-9_-]*’,
‘action’ => ‘[a-zA-Z][a-zA-Z0-9_-]*’,
‘trailingSlash’ => ‘/’,
),
‘defaults’ => array(
‘controller’ => ‘Index’,
‘action’ => ‘index’,
),
),
And `urlFor()` will not add the trailing slash. It’s still a bit hacky, but seems like the best solution to me.
I don’t know if it’s just me or if everyone
else encountering problems with your website.
It appears like some of the written text within your posts are running off the screen.
Can somebody else please provide feedback and let me know if this is happening to them as well?
This could be a problem with my web browser because I’ve had this happen previously.
Thanks
[…] https://samsonasik.wordpress.com/2012/07/24/zend-framework-2-trailing-slash/ […]