Reputation: 873
I'm setting up a classic symfony 4.3 project (aka a web site) in multi-languages and all routes are OK. I would like to expose some data with an simple API REST (only 3,4 routes with method GET) so i have installed FOSRestBundle 2.5. I know this version is compatible with SF 4.*.
The problem is my API routes return all 404 BUT NOT the "web" routes. I fllow symfony doc and FOSREST doc and some tutorials.
In fos_rest.yaml, i have declared the two types of routes in rules
argument. I have separeted the controllers for web and controller for API in two directories (the two directories are mount in services.yaml)
Here the FOS REST configuration : config/packages/fos_rest.yaml
fos_rest:
routing_loader:
default_format: json
include_format: false
body_listener: true
format_listener:
rules:
- { path: '^/api', priorities: [ json ], fallback_format: json, prefer_extension: false }
- { path: '^/', priorities: [ 'html', '*/*'], fallback_format: ~, prefer_extension: true }
param_fetcher_listener: true
access_denied_listener:
json: true
view:
view_response_listener: 'force'
formats:
json: true
config/services.yaml:
services:
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
App\ControllerApi\:
resource: '../src/ControllerApi'
tags: ['controller.service_arguments']
And annotations definitions : config/routes/annotations.yaml
# https://symfony.com/doc/current/routing.html#localized-routing-i18n
web_controllers:
resource: ../../src/Controller/
type: annotation
prefix:
en: /
fr: /{_locale}
de: /{_locale}
es: /{_locale}
pt: /{_locale}
requirements:
_locale: '%app_locales%'
defaults:
_locale: '%locale%'
# Controller for Rest API
rest_controller:
resource: ../../src/ControllerApi/
type: annotation
prefix: /api
Web controllers are basics and extends AbstractController
. For API controller, here an exemple :
namespace App\ControllerApi;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
final class fooApiController extends AbstractFOSRestController
{
/**
* @param string $id
*
* @return View
*
* @Rest\Get("/demo/{$id}", name="api_demo")
*/
public function getSimpleDataById($id)
{
/** @var Dso $dso */
$data = $this->myCustomManager->getData($id);
if (!$data instanceof CustomEntity) {
throw new NotFoundHttpException();
}
return View::create($data, Response::HTTP_OK);
}
}
Here's an exemple of route generated (example with homepage) :
www-data@php:~/symfony$ php bin/console debug:router | grep home
homepage.en ANY ANY ANY /
homepage.fr ANY ANY ANY /{_locale}/
homepage.de ANY ANY ANY /{_locale}/
homepage.es ANY ANY ANY /{_locale}/
homepage.pt ANY ANY ANY /{_locale}/
All is ok (and others routes for web too).
Here's with API:
www-data@php:~/symfony$ php bin/console debug:router | grep api
api_demo GET ANY ANY /api/demo/{$id}
So i curl the two :
curl --head http://symfony.local
HTTP/1.1 200 OK
Server: nginx
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.2.14
Set-Cookie: PHPSESSID=336b94195892ebb5a3b4e7f2d2799fbb; path=/; HttpOnly; SameSite=lax
Cache-Control: max-age=0, must-revalidate, private, s-maxage=3600
Date: Fri, 19 Jul 2019 13:26:05 GMT
X-Debug-Token: 5653c7
# Works with others URL...
curl --head http://symfony.local/api/demo/m42
HTTP/1.1 404 Not Found
Server: nginx
Content-Type: application/json
Connection: keep-alive
X-Powered-By: PHP/7.2.14
Cache-Control: max-age=0, must-revalidate, private
Date: Fri, 19 Jul 2019 13:27:03 GMT
I don't find the error. Thanks for reading and answering
Upvotes: 0
Views: 2195
Reputation: 873
I have found a starting solution. I have changed my conf on fos_rest.yaml :
fos_rest:
routing_loader:
default_format: json
include_format: false
body_listener: true
format_listener:
rules:
- { path: '^/api', priorities: [ json ], fallback_format: json, prefer_extension: true }
param_fetcher_listener: true
access_denied_listener:
json: true
view:
view_response_listener: true
formats:
json: true
zone:
- { path: ^/api/* }
I have removed the rule '^/' and adding the zone. And the 404 wasn't here.
There was an error too on annotation in controller: Here's the correct syntax :
/**
* @Rest\View()
* @Rest\Get("/demo/{id}", name="api_demo")
*
* @param string $id
*
* @return View
*/
public function getSimpleDataById(string $id): View
Upvotes: 0