Reputation: 111839
I have a very simple bundle controller
<?php
namespace Mnab\ContactBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
public function indexAction($anything)
{
return new \Symfony\Component\HttpFoundation\Response(__CLASS__ .' '.$anything);
//return $this->render('MnabContactBundle:Default:index.html.twig', array('name' => $anything));
}
}
I have roter config in YML files
CASE 1: My routing for this controller looks like:
mnab_contact_homepage:
pattern: contact/{anything}
defaults: { _controller: MnabContactBundle:Default:index, anything: null }
requirements:
anything: .*
In this case application accepts for this controller the following urls:
/contact /contact/ /contact/bla /contact/bla/bla/blabla
and so on
CASE 2 My routing for this controller looks like:
mnab_contact_homepage:
pattern: contact/
defaults: { _controller: MnabContactBundle:Default:index, anything: null }
requirements:
anything: .*
In this case application accepts only
/contact/
and
if there is url:
/contact -> it makes redirection (dont' know what type 301 or 302) to /contact/ url
CASE 3 My routing for this controller looks like:
mnab_contact_homepage:
pattern: contact
defaults: { _controller: MnabContactBundle:Default:index, anything: null }
requirements:
anything: .*
In this case application accepts only
/contact
Expected results
In case 3 everything is as I expect but case 1 and 2 works not as I expect
Questions
Why in case 1 application accepts also /contact url ? I have clearly expect /contact/ url not contact/
Why in case 2 applications accepts also /contact url making redirection for it to /contact/
Why behaviour is different in case 1 and case 2
Is it possible to change this behaviour - for example not accept at all /contact in my case ? (In fact I don't mind because I want some urls with trailing slash but in case 1 I'll have to do redirection for version without trailing slash)
Does Symfony do such "tricks" for all urls (also with extension as for example I define it works for /test.html and Symfony makes it work also for /test.html/ ) or only for those without extension?
Where can I read a bit more about such cases?
Upvotes: 1
Views: 1297
Reputation:
Why in case 1 application accepts also /contact url? I have clearly expect /contact/ url not contact/
/contact
means that you are leaving out the anything
parameter which will be in this case null
.
/contact/
means that you are NOT leaving out the anything
parameter. In this case NotFoundHttpException
should be thrown, because you haven't provided any value for anything
parameter.
Why in case 2 applications accepts also /contact url making redirection for it to /contact/
It is behaviour of Symfony2 routing component, there can't be really told more about it.
Why behaviour is different in case 1 and case 2
Because in case 1 there is a parameter in the route. The parameter has although a default value. In case 2 there is no parameter in the route.
Is it possible to change this behaviour - for example not accept at all /contact in my case
Yes, for example make the anything
parameter to be mandatory, not optional.
Does Symfony do such "tricks" for all urls (also with extension as for example I define it works for /test.html and Symfony makes it work also for /test.html/ ) or only for those without extension
Behaviour of the routing component is consistent, it does not matter if you use test
or test.html
.
Where can I read a bit more about such cases?
There won't be many sources regarding this, but you can always check out source code of the routing component.
However, documentation to routing component can be found here Routing.
Upvotes: 2