确保安装了Symfony 5包和Composer。
安装FOS REST包:composer require friendsofsymfony/rest-bundle。
配置FOS REST包:在config/bundles.php文件中添加以下行:
FOS\RestBundle\FOSRestBundle::class => ['all' => true],
配置FOS REST包:在config/routes/annotations.yaml文件中添加以下行:
fos_rest:
resource: "@FOSRestBundle/Resources/config/routing.yaml"
在控制器中使用FOS REST Bundle:
namespace App\Controller;
use FOS\RestBundle\Controller\AbstractFOSRestController; use FOS\RestBundle\Controller\Annotations as Rest; use Symfony\Component\HttpFoundation\Response;
class ApiController extends AbstractFOSRestController { /** * @Rest\Get("/api") */ public function getApi(): Response { return new Response('API Response from FOS Rest Bundle'); } }
执行Symfony 5命令:php bin/console cache:clear。
在浏览器中访问:http://localhost:8000/api,应该能看到“API Response from FOS Rest Bundle”的响应。