带有注解的最小symfony设置

问题描述 投票:1回答:3

我想基于Symfony框架创建一个简单的api。

控制器已经实现。

1)我需要的作曲家软件包的最低设置是什么?

2)如何设置一个有效的index.php文件,该文件从注释创建路由,匹配URL并输出响应?

非常感谢!

这是位于src / Bitter / Cloud / Server / Controller / PhotosController.php中的我的一个控制器的示例代码:]

<?php

namespace Bitter\Cloud\Server\Controller;

use Bitter\Cloud\Cloud;
use Bitter\Cloud\Services\PhotosService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class PhotosController extends AbstractController
{
    protected Request $request;
    protected PhotosService $service;

    public function __construct()
    {
        $this->request = Request::createFromGlobals();
        $this->service = Cloud::getServices()->getPhotos();
    }

    /**
     * Send a request to fetch photos and videos from the photo service.
     *
     * @Route("/photos/get_photos")
     *
     * @link project://docs/services/photos/get-photos.md
     */
    public function getPhotos(): JsonResponse
    {
        $this->service->init();

        return new JsonResponse([
            "success" => true,
            "photos" => $this->service->getPhotos()
        ]);
    }
}

所有其他控制器都是相似的。

这是我的composer.json设置:

{
  "name": "bitter/cloud-server",
  "description": "Cloud Server for PHP.",
  "license": "MIT",
  "type": "project",
  "homepage": "*********removed*********",
  "authors": [
    {
      "name": "*********removed*********",
      "email": "*********removed*********",
      "role": "Developer"
    }
  ],
  "minimum-stability": "dev",
  "repositories": [
    {
      "name": "bitter/cloud-api",
      "type": "vcs",
      "url": "[email protected]:*********removed*********"
    }
  ],
  "keywords": [
    "cloud",
    "php",
    "api"
  ],
  "support": {
    "issues": "*********removed*********"
  },
  "require": {
    "bitter/cloud-api": "*",
    "symfony/http-foundation": "5.0.0",
    "symfony/routing": "5.0.0",
    "symfony/config": "5.0.0",
    "doctrine/annotations": "1.8.0",
    "symfony/framework-bundle": "5.0.0",
    "doctrine/cache": "1.8.0"
  },
  "autoload": {
    "psr-4": {
      "Bitter\\Cloud\\Server\\": "src/Bitter/Cloud/Server"
    }
  }
}

symfony annotations
3个回答
0
投票

有很多方法可以在作曲家中使用API​​,其中最知名的是https://symfony.com/doc/master/bundles/FOSRestBundle/index.html fosRestBundle和https://symfony.com/projects/apiplatform,这取决于您的需要,所显示的代码是api的基本概念


0
投票

[Apiplatform可能是构建基于Symfony的API的最佳解决方案,尽管它是完整的系统(json,json + ld,graphql,auth,文档,分页等。]。

或者如果您只需要基本设置,则您的解决方案可能会很好(假设您使用的是Symfony skeleton)。只需在控制器的注释中设置路由并以JSON格式返回数据即可。

至于您需要的捆绑包,取决于您想要实现的目标。例如,如果您需要身份验证,则security bundle可能是一个好主意。请参见this doc在Symfony中设置API的身份验证。

maker bundle对于创建实体和迁移非常有用。 Doctrine ORM bundle非常便于从数据库中存储和检索数据并进行维护。但是,这完全取决于您自己进行选择,请参见Symfony中的一些popular bundles


0
投票

您需要做的就是使用作曲家创建一个简单的应用程序:

composer create-project symfony/skeleton

然后添加注释包:

composer require annotations

© www.soinside.com 2019 - 2024. All rights reserved.