带注释的最小 symfony 设置
Minimal symfony setup with annonations
我想创建一个基于 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": "git@bitbucket.org:*********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"
}
}
}
有很多方法可以将 API 与 composer 一起使用,最广为人知的是 https://symfony.com/doc/master/bundles/FOSRestBundle/index.html fosRestBundle and https://symfony.com/projects/apiplatform 这取决于您的需要 您显示的代码是 api[=11 的基本概念=]
Apiplatform 可能是构建基于 Symfony 的 API 的最佳解决方案,尽管它是一个完整的系统(json、json+ld、graphql 、授权、文档、分页等...)。
或者,如果您只需要一个基本设置,您的解决方案可能没问题(假设您使用的是 Symfony skeleton)。只需在控制器的注释中设置路由和 JSON 格式的 return 数据。
至于你需要什么包,就看你想达到什么目的了。
security bundle might be a good idea if you need authentication for instance. See this doc 在 Symfony 中为 API 设置身份验证。
Symfony 中的 maker bundle is very useful to create entities and migrations. The Doctrine ORM bundle is very handy to store and retrieve data from your database and maintain it. But it's all up to you to chose from these, see some popular bundles。
您需要做的就是使用 Composer 创建一个简单的应用程序:
composer create-project symfony/skeleton
然后添加一个注解包:
composer require annotations
我想创建一个基于 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": "git@bitbucket.org:*********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"
}
}
}
有很多方法可以将 API 与 composer 一起使用,最广为人知的是 https://symfony.com/doc/master/bundles/FOSRestBundle/index.html fosRestBundle and https://symfony.com/projects/apiplatform 这取决于您的需要 您显示的代码是 api[=11 的基本概念=]
Apiplatform 可能是构建基于 Symfony 的 API 的最佳解决方案,尽管它是一个完整的系统(json、json+ld、graphql 、授权、文档、分页等...)。
或者,如果您只需要一个基本设置,您的解决方案可能没问题(假设您使用的是 Symfony skeleton)。只需在控制器的注释中设置路由和 JSON 格式的 return 数据。
至于你需要什么包,就看你想达到什么目的了。 security bundle might be a good idea if you need authentication for instance. See this doc 在 Symfony 中为 API 设置身份验证。
Symfony 中的 maker bundle is very useful to create entities and migrations. The Doctrine ORM bundle is very handy to store and retrieve data from your database and maintain it. But it's all up to you to chose from these, see some popular bundles。
您需要做的就是使用 Composer 创建一个简单的应用程序:
composer create-project symfony/skeleton
然后添加一个注解包:
composer require annotations