在 Slim 3 中使用支持自动装配的 league/container 作为核心容器

v0.1.0 2020-12-21 14:31 UTC

This package is auto-updated.

Last update: 2024-09-21 22:36:18 UTC


README

通过将默认的 Slim 容器替换为 PHP League's Container,Lean 提供了服务提供者、自动装配等功能。

许可证

许可协议为 MIT。完全免费,适用于私人或商业项目。

安装

$ composer require andrewdyer/lean

使用

开始使用 Lean 的最简单方法就是创建一个 Anddye\Lean\App 实例

require 'vendor/autoload.php';

$app = new \Anddye\Lean\App();

$app->get('/hello/{name}', function (Request $request, Response $response, string $name) {
    return $response->write('Hello, ' . $name);
});

$app->run();

在幕后,通过将所有必需的 Slim 组件添加到 League 的容器中,来引导一个 Slim 应用程序。

服务提供者

服务提供者可以组织您的容器定义,并在大型应用程序中提高性能,因为服务提供者内注册的定义将在检索服务时懒加载。

要构建服务提供者,只需扩展基本的服务提供者并定义您想要注册的内容即可。

use League\Container\ServiceProvider\AbstractServiceProvider;

class SomeServiceProvider extends AbstractServiceProvider
{
    /**
     * The provided array is a way to let the container
     * know that a service is provided by this service
     * provider. Every service that is registered via
     * this service provider must have an alias added
     * to this array or it will be ignored.
     */
    protected $provides = [
        SomeInterface::class,
    ];

    /**
     * This is where the magic happens, within the method you can
     * access the container and register or retrieve anything
     * that you need to, but remember, every alias registered
     * within this method must be declared in the `$provides` array.
     */
    public function register()
    {
        $this->getContainer()
            ->add(SomeInterface::class, SomeImplementation::class);
    }
}

要向容器注册此服务提供者,只需将您的提供者实例或完全限定的类名传递给 League\Container\Container::addServiceProvider 方法。

$app = new \Anddye\Lean\App();
$app->getContainer()->addServiceProvider(\Acme\ServiceProvider\SomeServiceProvider::class);

更多关于服务提供者的信息请点击 这里

设置

您可以通过容器上的 settings 键访问 Slim 的内部配置

$app = new \Anddye\Lean\App();

$app->getContainer()->get('settings')['displayErrorDetails'] = true;

或者,注册了一个别名,允许以更流畅的方式处理设置

$app = new \Anddye\Lean\App();

$app->getContainer()->get(\Slim\Settings::class)->set('displayErrorDetails', true);

更多关于可用的配置选项请点击 这里

路由参数

默认情况下,Lean 会使用方法注入将参数传递给您的路由。这允许您在方法级别对依赖项进行类型提示(类似于 Laravel 框架)。

路由参数将作为单独的参数传递给您的函数

$app->get('/books/{id}', function (Request $request, Response $response, string $id) {
    ...
});

它们也可以通过 getAttribute 方法访问。

$app->get('/books/{id}', function (Request $request, Response $response) {
    $id = $request->getAttribute('id');
    ....
});

如果您想禁用此行为并使用默认的 Slim 路由参数方式,可以将 methodInjection 设置为 false

$app->getContainer()->get(\Slim\Settings::class)->set('methodInjection', false);

更多关于路由的信息请点击 这里

错误处理器

默认情况下,Lean 使用 Slim 的错误处理器。Slim 实现错误处理器的不同方式,更多信息请点击 这里

通常,您会创建一个自定义的错误处理器类,如下所示

class CustomErrorHandler
{
    public function __invoke(ServerRequestInterface $request, Response $response, Throwable $exception)
    {
        return $response->withJson([
            'error' => 'Something went wrong',
        ], 500);
    }
}

然后通过将其添加到容器中来覆盖默认处理器

$app = new Anddye\Lean\App();

$app->getContainer()->share('errorHandler', function () {
    return new CustomErrorHandler();
});

理想情况下,您会将此代码放入服务提供者中。更多关于服务提供者的信息请参见上面的说明。

测试

$ php ./vendor/bin/phpunit