Configuration: Route FosRest and Attributes
jgauthi opened this issue · 2 comments
On my current project (Symfony 6.2, FosRestBundle 3.5), I have a config file for the FosRest route:
✔️ Annotation version:
# config/routes/fos_rest.yaml
api:
resource: ../../src/Controller/Api
type: annotation
prefix: /api
❌ I want to edit this file for attribute support, but it's not work :(.
# config/routes/fos_rest.yaml
api:
resource: ../../src/Controller/Api
type: attribute
prefix: /api
# -- OR ---
api:
resource:
path: ../../src/Controller/Api
namespace: App\Controller\Api
type: attribute
prefix: /api
The symfony config/routes.yaml use the standard code from Symfony 6, exemple.
When I check with php bin/console debug:router
, the routes does not use the Api prefix in url:
-------------------------------- ---------- -------- ------ -----------------------------------
Name Method Scheme Host Path
-------------------------------- ---------- -------- ------ -----------------------------------
app_api_dossierrest_getall GET ANY ANY /dossier/ <-- Instead of /api/dossier/
app_api_dossierrest_getone GET ANY ANY /dossier/{id} <-- Instead of /api/dossier/{id}
-------------------------------- ---------- -------- ------ -----------------------------------
The Rest controller:
<?php
// src/Controller/Api/DossierRestController.php
namespace App\Controller\Api;
use FOS\RestBundle\Controller\{AbstractFOSRestController, Annotations as Rest};
use FOS\RestBundle\View\View;
use Symfony\Component\HttpFoundation\Request;
#[Rest\Route('/dossier')]
class DossierRestController extends AbstractFOSRestController
{
#[Rest\Get('/'), Rest\View(serializerGroups: ['Dossier'])]
public function getAll(Request $request): View
{
How to edit the "config/routes/fos_rest.yaml" file for support attributes and prefix ?
@jgauthi I just checked on my project, I don't have such issue.
I checked with:
- SF6.2.14 / FosRestBundle 3.5.0
- SF6.3.4 / FosRestBundle 3.5.0
api:
resource: "@PTCApiBundle/Controller/"
type: attribute
prefix: /api
The Rest controller:
<?php
namespace PTC\ApiBundle\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
...
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Rest\Route('/stats')]
class StatisticController extends AbstractController
{
#[Rest\View('')]
#[Rest\Get('/registration/{id}', requirements: ['id' => '\d+'])]
public function registrationAction(?int $id = null): RegistrationTO
{
php bin/console debug:router --show-controllers ptc_api_statistic_registration
+--------------+---------------------------------------------------------------------------------+
| Property | Value |
+--------------+---------------------------------------------------------------------------------+
| Route Name | ptc_api_statistic_registration |
| Path | /api/stats/registration/{id} |
| Path Regex | {^/api/stats/registration(?:/(?P<id>\d+))?$}sD |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | GET |
| Requirements | id: \d+ |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: PTC\ApiBundle\Controller\StatisticController::registrationAction() |
| | id: NULL |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------------------------------+
I checked with my controler extending AbstractController
or AbstractFOSRestController
but it's the same, it doesn't matter.
Thank for your response.
After several tests, I found the solution.
The content of the file config/routes.yaml has been commented. Instead, I create a new file config/routes/controller.yaml with the same content (with additionnal ../
in path value). The api route configuration work and classic controller too.