laravel-bridge / slim
The Laravel Bridge use on Slim framework
Requires
- php: ^7.1
- ext-json: *
- http-interop/http-factory-slim: ^2.0
- illuminate/container: ^5.6 | ^6 | ^7
- illuminate/support: ^5.6 | ^6 | ^7
- laravel-bridge/container: ^1.1
- laravel-bridge/scratch: ^1.6
- laravel-bridge/support: ^1.0
- mileschou/psr: ^1.0
- psr/container: ^1.0
- psr/http-factory: ^1.0
- slim/slim: ^3.0
- symfony/debug: ^4.4 | ^5
- symfony/psr-http-message-bridge: ^1.3 | ^2.0
Requires (Dev)
- illuminate/config: ^5.6 | ^6 | ^7
- illuminate/http: ^5.6 | ^6 | ^7
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^7 | ^8 | ^9
- psy/psysh: ^0.10.2
- squizlabs/php_codesniffer: ^3.5
Suggests
- illuminate/config: Suggest when use Laravel settings
- illuminate/http: Suggest when use Laravel or Symfony request instance
README
Laravel Bridge for Slim Framework
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');