2
votes

I am novice at Symfony. Current version : 2.8.16 My problem : I don't manage redirection.

app/config/routing.yml :

gb_platform:
resource: "@GBPlatformBundle/Resources/config/routing.yml"
prefix:   /platform

src/GB/PlatformBundle/Ressources/config/routing.yml :

gb_platform_home:
path :   /{page}
defaults: 
    _controller : GBPlatformBundle:Advert:index
    page: 1
requirements:
    page: \d*

gb_platform_view:
path :   /advert/{id}
defaults: { _controller : GBPlatformBundle:Advert:view }
requirements:
    id: \d+

src/GB/PlatformBundle/Controller/AdvertController.php :

namespace GB\PlatformBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

class AdvertController extends Controller
{
    public function indexAction()
    {
        $url = $this->get('router')->generate(
            'gb_platform_view',
            array( 'id' => 5)
            );
        return new Response("L'url de l'annonce d'id 5 est : ". $url);
    }

    public function viewAction($id)
    {
        return $this->redirectToRoute('gb_platform_home');
        //return new Response("lol");
    }
}

The error is the following :

Unable to generate a URL for the named route "gb_platform_home" as such route does not exist.

I can access manually to both route ("app_dev.php/platform/" can be reached). But the URL generation is only working for gb_platform_view.

I supposed the source of the problem could be in app/config/routing.yml file specifying no itteration ... but if I do so it doesn't solve anything and seems to create more problems.

I find the debugging route command line :

php app/console debug:router

Here is the result.

 Screenshot shell debug router

gb_platform_home is clearing identify as a know route so I don't understand. Also, you may notice there's a shifted unique print for this route in the shell. Why ? Key for solving ?

Could a 4 spaces encoding be the problem ?

Thanks for your help.

1
I think that you need to specify page value with your route usage. Something like return $this->redirectToRoute('gb_platform_home', array('page'=>0));Piyush Singh
Can you try that dump($this->get('router')->getRouteCollection()); and check gb_platform_home route?Fatih Kahveci
Piyush Singh : Problem unresolved as I expected because there is already default value for {page}.Doshibu
You can try for debugging using /platform/index/{page} instead. Sometimes the routing sees it as ambiguous when the variable is not explicitly defined in the route.Piyush Singh
I know it's a silly question: but why are characters from the gb_platform_home line slightly to the left, compared to the rest? looks unusual. It came like this directly, or was this edited in any way?yivi

1 Answers

3
votes

This solution below should work as you expect.

Requirement and Default value will not work as you expect when generating route but rather when parsing the user entered url

your controler will need to be define like this:

// you must set the default value like that `$page = 1`
function indexAction(Request $request, $page = 1) 
gb_platform_home:
  path :   /{page}
  defaults: 
    _controller: GBPlatformBundle:Advert:index
    page: 1
  requirements:
    page: \d*
gb_platform_home_: <------ use it to generate your empty route
  path :   /
  defaults: { _controller: GBPlatformBundle:Advert:index }

Cause of things like that i prefer using route as annotation

  • First rules is the one that will trigger when we receive the url with a {page} , you can name the route if you need
  • Second rule same as first but we accept trailing slash
  • Third rule to generate your empty route
/**
 *  @Route("/platform/{page}", requirements={"page": "\d*"} ,defaults={"page":1})
 *  @Route("/platform/{page}/", requirements={"page": "\d*"}, defaults={"page":1}) 
 *  @Route("/platform", name="gb_platform_home")
 */
function indexAction(Request $request, $page = 1)
{
}