定制 jangolle/slim-symfony-container 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

jangolle/slim-symfony-container

Composer 安装命令:

composer require jangolle/slim-symfony-container

包简介

Simple SlimPHP container integration with Symfony dependency injection container

README 文档

README

Build Status codecov PHP from Packagist Packagist

Slim with Symfony DI Container integration

Easily resolve Symfony ContainerBuilder and setup in it all default Slim Application dependencies if necessary. Loader resolve symfony configuration params and setup slim default settings with params to symfony ParameterBag.

Installation

Library is available on Packagist.

Installation via composer is the recommended way to install it.

Just add this line to required section of your composer.json file:

"jangolle/slim-symfony-container": "~1.0"

or just run in console

cd /path/to/your/project
composer require jangolle/slim-symfony-container

Default usage

You can directly create ContainerBuilder and use it from scratch to setup with SlimDefaultServicesInjection like this:

<?php
declare(strict_types=1);

use JanGolle\SlimSymfonyContainer\ContainerManager;
use JanGolle\SlimSymfonyContainer\Loader\SlimDefaultServicesInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;

$containerManager = new ContainerManager();
$container = new ContainerBuilder();

$container = $containerManager->resolveContainer($container, new SlimDefaultServicesInjection());

$app = new \Slim\App($container);

//setup routes or something

$app->run();

Use with Symfony configs

If you want your symfony configuration files in your project you can actually do something like:

<?php
declare(strict_types=1);

use JanGolle\SlimSymfonyContainer\ContainerManager;
use JanGolle\SlimSymfonyContainer\Loader\SlimDefaultServicesInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

$containerManager = new ContainerManager();
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/path/to/config'));
$loader->load('services.yaml');

$container = $containerManager->resolveContainer($container, new SlimDefaultServicesInjection());

$app = new \Slim\App($container);

//setup routes or something

$app->run();

Your services.yaml parameters block might look like this:

parameters:
  slim.settings.httpVersion: '1.1'
  slim.settings.responseChunkSize: 4096
  slim.settings.displayErrorDetails: !php/const DEBUG

NOTE: You can override default SLIM settings in your config file with your custom values.. or not, it's up to you :) All SLIM necessary settings will be applied to container injection with your params or not.

Custom wrapper class

If you have your own container that is instance of Symfony\Component\DependencyInjection\ContainerBuilder and you want to setup it with Slim dependencies you can do like this:

$container = new YourCustomContainer($fullOfParams);//instance of Symfony\Component\DependencyInjection\ContainerBuilder
$container = $containerManager->resolveContainer($container, new SlimDefaultServicesInjection());

or you can just instantiate it with default constructor via ::class as first arg of resolveContainer:

$container = $containerManager->resolveContainer(YourCustomContainer::class, new SlimDefaultServicesInjection());

Container access inside app

Inside your routes scope $this will return our container which is actually instance of Symfony\Component\DependencyInjection\ContainerBuilder

//...
$app->get(
    '/your/route',
    function (Request $request, Response $response, array $args) {
        $this->get('someService')->doSomeStuff();

        return $response;
    }
);

PhpStorm Symfony Plugin support

If you are using PhpStorm IDE you can install in it Symfony plugin and get access to typehinting and IDE autocomplite for services.

Thanks for your attention!

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固