Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Routes of the form /hello/{name} with an optional parameter at the end don't match as expected #5869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chrismcmacken opened this issue Oct 30, 2012 · 16 comments

Comments

@chrismcmacken
Copy link

This issue can be reproduced with any brand new install of Symfony 2.1.2, just generate a default bundle and add the below route.

routing.yml file

my_test_homepage:
    pattern:  /hello/{name}
    defaults: { _controller: MyTestBundle:Default:index, name: DefaultName }
DefaultController.php

<?php

namespace My\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction($name)
    {
        return $this->render('MyTestBundle:Default:index.html.twig', array('name' => $name));
    }
}

The route will be matched on any urls of the form http://localhost/hello/test (or other strings after hello) but the route will not be matched on urls of the form http://localhost/hello/ even though the parameter should be optional, this example is practically straight out of the Symfony2 documentation so as far as I can tell that's how it should behave.

@mahmouds
Copy link
Contributor

It matches the route "http://localhost/hello" without the extra "/" at the end
please use symfony's generate url function to get the correct url

@mvrhov
Copy link

mvrhov commented Oct 30, 2012

AFAIK this case is handled in one of the routing component PRs

@chrismcmacken
Copy link
Author

@mahmouds You are correct, it does match without the trailing slash, but that doesn't match the pattern as it's defined. The pattern should be /hello/{optionalParameter}, not /hello{/optionalParameter}. If I use the generateUrl() function it does return /hello as the relative url, but that's still wrong.

@mvrhov Can you please clarify PR?

@mahmouds
Copy link
Contributor

@chrismcmacken Here is the PR #5410

@chrismcmacken
Copy link
Author

@mvrhov @mahmouds That PR doesn't fix this issue as far as I can tell.

@Tobion
Copy link
Contributor

Tobion commented Oct 30, 2012

You are correct, it does match without the trailing slash, but that doesn't match the pattern as it's defined. The pattern should be /hello/{optionalParameter}, not /hello{optionalParameter}. If I use the generateUrl() function it does return /hello as the relative url, but that's still wrong.

@chrismcmacken /hello/{optionalParameter} matches /hello and /hello/.... Not /hello/ because the / is considered a seperator that is left out without the optional parameter. Your comparison with /hello{optionalParameter} is not valid here. The generated path /hello is correct and it's not a "relative url" either. Its an abolute path.

Furthermore, #5410 has nothing to do with this use case here. Your use case can be achieved by with #5424.
You can close this issue as this is not a bug but a feature request that is already covered by the linked issue.

@chrismcmacken
Copy link
Author

@Tobion Yeah, this was a misunderstanding on my part as to the way the routing configuration works. I'm closing it as invalid, thanks for the help. It might be worth noting that in the documentation though.

@Tobion
Copy link
Contributor

Tobion commented Oct 30, 2012

Then please open an issue on symfony-docs that mentions that handling of optional parameters should be clarified.

@chrismcmacken
Copy link
Author

Will do, I'll write it up tonight. Thanks again @Tobion

@Tobion
Copy link
Contributor

Tobion commented Oct 30, 2012

No problem.

@jesmarquez
Copy link

I have a issue in this code:

return $this->redirect($this->generateUrl('acme_hello_homepage',array('name'=>$nombre)));

i have this error:
FatalErrorException: Error: Cannot use object of type Acme\TaskBundle\Entity\Task as array in /var/www/Symfony/src/Acme/TaskBundle/Controller/DefaultController.php line 31

@hacfi
Copy link
Contributor

hacfi commented Jan 9, 2014

@jesmarquez Is $nombre an object? Has to be some scalar value.

@chrismcmacken
Copy link
Author

Why are you posting this in a closed issue that was opened on documentation?

@jesmarquez
Copy link

$nombre i get using the getData() method, this is the code: 
 
// data is an array with "name", "email", and "message" keys
$data = $form->getData();

$nombre = $data['task'];

El Jueves 9 de enero de 2014 13:56, hacfi [email protected] escribió:

@jesmarquez Is $nombre an object? Has to be some scalar value.

Reply to this email directly or view it on GitHub.

@hacfi
Copy link
Contributor

hacfi commented Jan 10, 2014

@jesmarquez Looks like your task field is an entity field so you get a Task object back which you can’t use for generating the url.

Try

return $this->redirect($this->generateUrl('acme_hello_homepage',array('name'=>$nombre->getId())));

@jesmarquez
Copy link

Thank hacfi, $nombre is a instance!

Excuse me by write a post here when is close!

El Viernes 10 de enero de 2014 18:24, hacfi [email protected] escribió:

@jesmarquez Looks like your task field is an entity field so you get a Task object back which you can’t use for generating the url.
Try
return $this->redirect($this->generateUrl('acme_hello_homepage',array('name'=>$nombre->getId())));

Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants