FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.79k stars 708 forks source link

Configuration: Route FosRest and Attributes #2394

Closed jgauthi closed 10 months ago

jgauthi commented 10 months ago

On my current project (Symfony 6.2, FosRestBundle 3.5), I have a config file for the FosRest route:

:heavy_check_mark: Annotation version:

# config/routes/fos_rest.yaml
api:
    resource: ../../src/Controller/Api
    type: annotation
    prefix: /api

:x: 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 ?

raziel057 commented 10 months ago

@jgauthi I just checked on my project, I don't have such issue.

I checked with:

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.

jgauthi commented 10 months ago

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.