DrKey
DrKey

Reputation: 3495

Symfony - Simple @Route doens't seem to work as expected

I just started on Symfony so I'm trying to learn Routing. Therefore, accordingly with official documentation about Routing, I made a simple page like this:

<?php
// src/AppBundle/Controller/MainController.php
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MainController extends Controller
{
    /**
    * @Route("/index", name="Players lister")
    */
    public function renderFirstPage()
    {
        return $this->render('accounts.html.twig');
    }

    /**
    * @Route("/index/{account}", name="Players lister")
    */
    public function getPlayersList($account)
    {
        $players = array("Player1", "Player2");
        return $this->render('accounts.html.twig', array(
            'account' => $account,
            'players' => $players
        ));
    }
}
?>

But when I go at http://localhost:8000/app_dev.php/index I get No route found for "GET /index" while the second Route works good. Instead, if I delete the second Route, the first one works.

What am I doing wrong?

Upvotes: 0

Views: 82

Answers (2)

shuba.ivan
shuba.ivan

Reputation: 4061

routing path and name should be uniq

/**
* @Route("/", name="home")
*/
public function renderFirstPage()
{
    return $this->render('accounts.html.twig');
}

/**
* @Route("/index/{account}", name=players_lister")
*/
public function getPlayersList($account)
{
    $players = array("Player1", "Player2");
    return $this->render('accounts.html.twig', array(
        'account' => $account,
        'players' => $players
    ));
}

Upvotes: 1

Grzegorz Gajda
Grzegorz Gajda

Reputation: 2474

Each route registered in Symfony's Routing component should have unique URI and name.

Each route also has an internal name: blog_list and blog_show. These can be anything (as long as each is unique) and don't have any meaning yet. Later, you'll use it to generate URLs.

Upvotes: 1

Related Questions