Home » Php » php – Struggling with annotations in Symfony

php – Struggling with annotations in Symfony

Posted by: admin February 25, 2020 Leave a comment


As mentioned in the title, I can’t even start working with annotations – cause all I get is Page Not Found when I try to open /lucky/number.
Here’s my code below.

    namespace App\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Routing\Annotation\Route;

    class BlogController{
             * @Route("/lucky/number")
            public function number(){
                    $number = random_int( 0, 100);
                    return new Response("<html><body>"."Number".$number."</body></html>");

How to&Answers:

As @Zeljka said you must have a unique route name for each route.
So you would have this

 * @Route("/my-custom-path", name="my-unique-custom-path")

I also do not get it how simple and straightforward thing like that would be hard.
I highly recommend re-reading the routing documentation and the symfony application flow.