承接 benycode/slim-jad-middleware 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

benycode/slim-jad-middleware

最新稳定版本:v1.0.2

Composer 安装命令:

composer require benycode/slim-jad-middleware

包简介

Jad (jsonapi.org) implementation in the slim 4

README 文档

README

Simplest way to have JSON:API endpoint in your Slim project

Table of contents

Install

Via Composer

$ composer require benycode/slim-jad-middleware

Requires Slim 4.

Usage

Use DI to inject the library Middleware classes:

use Psr\Container\ContainerInterface;
use Jad\Configure;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
use Doctrine\DBAL\Configuration as DoctrineConfiguration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use BenyCode\Slim\JadMiddleware\JsonApiMiddleware;

return [
    ......
  JsonApiMiddleware::class => function (ContainerInterface $container): JadApiMiddleware {
		
    $config = Configure::getInstance();

      $config
        ->setConfig('strict', true)
        ;

      return new JsonApiMiddleware(
        $container->get(EntityManager::class), 
        $container->get(ResponseFactoryInterface::class),
        $config,
        '/api/json', // endpoint path, middleware will catch requests etc: /api/json/videos, /api/json/videos/1.....
      );
  },
  EntityManager::class => static function (ContainerInterface $container): EntityManager {
    /** @var array $settings */
    $settings = $container->get('settings');
    
    // Use the ArrayAdapter or the FilesystemAdapter depending on the value of the 'dev_mode' setting
    // You can substitute the FilesystemAdapter for any other cache you prefer from the symfony/cache library
    $cache = $settings['doctrine']['dev_mode'] ?
      DoctrineProvider::wrap(new ArrayAdapter()) :
      DoctrineProvider::wrap(new FilesystemAdapter(directory: $settings['doctrine']['cache_dir']));

      $config = Setup::createAttributeMetadataConfiguration(
        $settings['doctrine']['metadata_dirs'],
        $settings['doctrine']['dev_mode'],
        null,
        $cache
      );

      return EntityManager::create($settings['db'], $config);
  },
];

add a Middlewares to route globaly:

use BenyCode\Slim\JadMiddleware\JsonApiMiddleware;

$app
  ...
  ->add(JsonApiMiddleware::class)
  ;
  ...

Doctrine config:

$settings['db'] = [
    'driver' => 'pdo_mysql',
    'host' => 'localhost',
    'dbname' => 'test',
    'user' => 'root',
    'password' => '',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'driverOptions' => [
        // Turn off persistent connections
        PDO::ATTR_PERSISTENT => false,
        // Enable exceptions
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        // Emulate prepared statements
        PDO::ATTR_EMULATE_PREPARES => true,
        // Set default fetch mode to array
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        // Set character set
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci'
    ],
];

$settings['doctrine'] = [
	// Enables or disables Doctrine metadata caching
	// for either performance or convenience during development.
	'dev_mode' => true,

	// Path where Doctrine will cache the processed metadata
	// when 'dev_mode' is false.
	'cache_dir' => APP_ROOT . '/../var/doctrine',

	// List of paths where Doctrine will search for metadata.
	// Metadata can be either YML/XML files or PHP classes annotated
	// with comments or PHP8 attributes.
	'metadata_dirs' => [APP_ROOT . '/../src/Domain'],
	
	'migrations' => [
		'migrations_paths' => [
			'App\\Doctrine\\Migration' => dirname(__DIR__) . '/resources/doctrine/migrations',
		],
		'custom_template' => dirname(__DIR__) . '/resources/doctrine/doctrine_migrations_class.php.tpl',
    ],
];

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2022-09-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固