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
ProductsController::index()
. – ehymelphp bin/console debug:router
to see what routes symfony thinks you have defined. – ehymel