The Laravel Bridge use on Slim framework

v1.1.0 2020-10-27 16:06 UTC

This package is auto-updated.

Last update: 2024-11-24 12:17:07 UTC


README

Laravel Bridge for Slim Framework

tests codecov codecov Latest Stable Version Total Downloads License

The bridge for Laravel in Slim framework

Installation

Using Composer to install package:

composer require laravel-bridge/slim

Using array as container

App params is array, like following code.

use Slim\App;

$container = [
    SomeClass::class => function() {},
]

$app = new App($container);

Replace class name Slim\App to bridge class LaravelBridge\Slim\App:

// Rename use class
use LaravelBridge\Slim\App;

It will work on most Slim project. Here has an example for more detail.

Using Container

App params is Container, like following code.

use Slim\App;
use Slim\Container;

$container = new Container();
$container[SomeClass::class] = function() {};

$app = new App($container);

Use ContainerBuilder is good for this case. The builder will build an instance of Scratch Application.

use LaravelBridge\Slim\ContainerBuilder;
use Slim\App;

$containerBuilder = new ContainerBuilder();

// Use builder mixin the Scratch Application / Laravel Container
$containerBuilder->singleton(SomeClass::class, function() {});

$containerBuilder->setupDatabase($conncetion)
    ->setupProvider(YourProvider::class);

// Register provider for Slim Framework
$containerBuilder
    ->useLaravelFoundHandler()
    ->useLaravelHttp();

// Build Container and bootstrap
$container = $containerBuilder->buildAndBootstrap();

$app = new App($container);

Alternatively, ContainerBuilder can use Pimple / Slim container, too.

use LaravelBridge\Slim\ContainerBuilder;
use Slim\App;
use Slim\Container;

$slimContainer = new Container();
$slimContainer['foo'] = 'bar';

$containerBuilder = new ContainerBuilder($slimContainer);

// Build Container and bootstrap
$container = $containerBuilder->buildAndBootstrap();

$container->make('foo') // Will return 'bar'

Using Laravel Services

LaravelBridge\Slim\App will use the Slim default service (e.g. Slim\Handlers\Error). If you want to use the Laravel Error handler, you can set the second argument. It will use all Laravel service defined in this bridge.

use LaravelBridge\Slim\App;

$app = new App([], true);

ContainerBuilder is like Bridge App:

use LaravelBridge\Slim\ContainerBuilder;

$app = new ContainerBuilder([], true);

foundHandler

The foundHandler in Slim is invoke when the route found.

This bridge implements a auto injection handler for call a callable, names RequestResponse. Use Laravel Service or call ContainerBuilder::useLaravelFoundHandler() can enable handler.

$container = (new ContainerBuilder())
    ->useLaravelFoundHandler()
    ->buildAndBootstrap();

$app = new App($container);

$app->get('/', function (IlluminateRequest $request, $args) {
    // Auto-inject Illuminate Request in clousre
});

callableResolver

This bridge implements a auto injection handler for new controller, names CallableResolver. Use Laravel Service or call ContainerBuilder::useLaravelCallableResolver() to enable.

class SomeController
{
    public function __construct(Dep $dep) {}

    public function __invoke() {}

    public function view() {}
}


$container = (new ContainerBuilder())
    ->useLaravelCallableResolver()
    ->buildAndBootstrap();

$app = new App($container);

// Will call SomeController::__invoke()
$app->get('/', 'SomeController');

// Will call SomeController::view()
$app->get('/', 'SomeController:view');

settings

Laravel Bridge use the Collection class default. Using Laravel Services or call useLaravelSettings() method on ContainerBuilder will use the Illuminate\Config\Repository class.

$container = (new ContainerBuilder())
    ->setSettings(['foo' => 'bar'])
    ->useLaravelSettings()
    ->buildAndBootstrap();

// Return a Repository instance
$container->get('settings');