api-code / base-symfony
API 是从 api-platform/api-platform 分支出来的
v1.0.2
2017-06-18 02:43 UTC
Requires
- php: >=7.0
- api-platform/core: ^2.0
- beberlei/doctrineextensions: ^1.0
- doctrine/doctrine-bundle: ^1.6
- doctrine/doctrine-cache-bundle: ^1.2
- doctrine/doctrine-migrations-bundle: ^1.0
- doctrine/orm: ^2.5
- dunglas/action-bundle: ^0.3
- friendsofsymfony/user-bundle: ^2.0
- incenteev/composer-parameter-handler: ^2.0
- jmose/command-scheduler-bundle: ^1.2
- lexik/jwt-authentication-bundle: ^2.4
- nelmio/api-doc-bundle: ^2.13
- nelmio/cors-bundle: ^1.4
- phpdocumentor/reflection-docblock: ^3.0
- sensio/distribution-bundle: ^5.0
- sensio/framework-extra-bundle: ^3.0.2
- symfony/monolog-bundle: ^3.0
- symfony/swiftmailer-bundle: ^2.3
- symfony/symfony: 3.2.*
Requires (Dev)
- api-platform/schema-generator: ^1.2
- behat/behat: ^3.1
- behat/mink: ^1.7
- behat/mink-browserkit-driver: ^1.3.1
- behat/mink-extension: ^2.2
- behat/symfony2-extension: ^2.1
- behatch/contexts: ^2.5
- doctrine/doctrine-fixtures-bundle: ^2.3
- sensio/generator-bundle: ^3.0
- symfony/phpunit-bridge: ^3.0
This package is not auto-updated.
Last update: 2024-09-20 20:16:08 UTC
README
composer create-project api-code/base-symfony YOUR_FOLDER -s dev
这是一个https://api-platform.com/的分支,用于个人使用,包括
- api平台的基本配置
- 集成JWT
- Doctrine迁移、数据集和扩展。
- JMose调度器用于cron作业
- 带有spool命令的电子邮件!
记住,适用于PHP >= 7.0
开始
bin/console doctrine:database:create bin/console doctrine:schema:create # or the best way for databse, use migrations!! bin/console doctrine:migrations :diff Generate a migration by comparing your current database to your mapping information. :execute Execute a single migration version up or down manually. :generate Generate a blank migration class. :migrate Execute a migration to a specified version or the latest available version. :status View the status of a set of migrations. :version Manually add and delete migration versions from the version table. bin/console assets:install bin/console fos:user:create # Important for a Token! bin/console server:start # For Jmose scheduler bin/console scheduler:execute # Email with spool, program a scheduler with this strategy bin/console swiftmailer:spool:clear-failures
更改JWT密钥
$ mkdir -p var/jwt # For Symfony3+, no need of the -p option
$ openssl genrsa -out var/jwt/private.pem -aes256 4096
$ openssl rsa -pubout -in var/jwt/private.pem -out var/jwt/public.pem
获取令牌
我们使用https://github.com/lexik/LexikJWTAuthenticationBundle
curl -X POST https://:8000/api/login_check -d _username=johndoe -d _password=test
头部信息
在每个请求中添加此头部信息:Authorization: Bearer tokenJWT
添加更多控制器
第一种选择
在app/config/routing.yml中使用yml定义路由
# app/config/routing.yml book_special: path: '/productsjairo/{id}/special' methods: ['GET'] defaults: _controller: 'AppBundle:Products:special' _api_resource_class: 'AppBundle\Entity\Product' _api_item_operation_name: 'special'
第二种选择
在每个控制器的每个操作中使用注解定义路由器
# In a controller /** * Example with annotations * @Route( * name="demo_special", * path="/demo/{id}/special", * defaults={"_api_resource_class"=Product::class, "_api_item_operation_name"="specialdemo"} * ) * @Method("GET") */ public function demoAction() { $em = $this->getDoctrine()->getManager(); $products = $em->getRepository(Product::class)->findAll(); return $products; }
订阅事件以修改请求
在src/AppBundle/EventSubscriber中添加一个名为ProductMailSubscriber.php的文件
<?php // other uses use Doctrine\ORM\EntityManager; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; final class ProductMailSubscriber implements EventSubscriberInterface { private $mailer; private $em; protected $authorizationChecker; protected $token; public function __construct(\Swift_Mailer $mailer, EntityManager $em, AuthorizationCheckerInterface $authorizationChecker, \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage $token_storage) { // Initilize vars!!! } public static function getSubscribedEvents() { return [ //KernelEvents::VIEW => [['sendMail', EventPriorities::POST_WRITE]], KernelEvents::VIEW => [['accionDemo', EventPriorities::POST_WRITE]] ]; }
有关更多事件https://api-platform.com/docs/core/events
添加自定义过滤器
<?php ... other use /** * Product * @ApiResource(attributes={"filters"={"regexp"}}) /--->>>Add filter regexp * @ORM\Table(name="product") * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository") */ class Product {
现在你可以在URL中这样做
../api/products?id=[1,2]&number=20&description=otr