0
votes

Hello I have a small problem, I created a ProductController and a default Controller, both of them return a Json Response but the default one is working while the Product controller is not working for me, I don't know why :

Here is my default Controller :

<?php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* @Route("/")
*/
class DefaultController extends AbstractController {

    /**
    * @Route("/", name="default")
    */
    function index(){
       return new JsonResponse([
           "empty"=>"empryt"
       ]);
    }
}

?>

I get the result on the browser when typing localhost on the url

Here is my ProductController :

<?php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* @Route("/products")
*/
class ProductController extends AbstractController {

    /**
    * @Route("/", name="defaultprod")
    */
    function index(){
        return new JsonResponse([
            ["id"=>1, "name"=>"Nike", "price"=>140],
            ["id"=>2, "name"=>"Puma", "price"=>110],
            ["id"=>3, "name"=>"NewB", "price"=>10]
        ]);
    }
}

?>

But as you see, I need to type 'localhost/products" so I can get my results, but I get this common error :

The requested URL /products was not found on this server.

Any help would be much appreciated.


I found a solution to my question, I created a new symfony project, and then tapped this command line :

composer require symfony/apache-pack

And did exactly what I did before and my routes are all working now.

More details here

1
The route you have defined in your annotation is "/products/gt" for the function ProductsController::index().ehymel
Yes that was something I added after, even localhost/products/gt does not workTaouBen
From a command line, try php bin/console debug:router to see what routes symfony thinks you have defined.ehymel
You have this question tagged with symfony version 1.4. Is that correct? I have no idea how routing annotations were done back then. You should really update if that is true, or fix you tags to this question.ehymel
I debugged : I get my routes in a table, the default one and /products but still dont know why it is not workingTaouBen

1 Answers

1
votes

I think you should remove the / in the route definition of action.

you should have @Route(name="defaultprod") instead of @Route("/", name="defaultprod").

Or, you should type localhost/products/ instead of localhost/products