germey / emissary
允许您轻松地在Slim 3的DI容器中使用Laravel Service Providers
v1.0.5
2016-11-26 09:13 UTC
Requires
- php: >=5.5.0
- illuminate/support: >=5.1
- slim/slim: >=3.0
This package is not auto-updated.
Last update: 2024-09-14 20:40:46 UTC
README
允许您轻松地在Slim 3的DI容器中使用Laravel Service Providers以及相关的门面。Emissary受到itsgoingd/slim-services的启发,并兼容Slim 3。
这是一个仍在进行中的工作,请自行承担风险。
安装
通过Composer进行安装
$ composer require germey/emissary "1.*"
与Illuminate/Database一起工作的示例
<?php require 'vendor/autoload.php'; use Slim\App; use Slim\Container; $config = [ 'settings' => [ 'database.fetch' => PDO::FETCH_CLASS, 'database.default' => 'mysql', 'database.connections' => [ 'mysql' => [ 'driver' => 'mysql', 'host' => 'mysql', 'port' => 3306, 'database' => '', 'username' => '', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ], ] ] ]; $providers = [ 'Illuminate\Database\DatabaseServiceProvider' ]; $aliases = [ 'DB' => 'Illuminate\Support\Facades\DB' ]; $app = new App(new Container($config)); $app->add(new \Germey\Emissary\Middleware($providers, $aliases)); $app->get('/', function ($request, $response, $args) { # Illuminate/Database via facade $tables = DB::select('SHOW TABLES'); var_dump($tables); # or via the container: $tables = $this->get('db')->select('SHOW TABLES'); var_dump($tables); }); $app->run();
自定义Service Provider
Example.php
<?php
namespace foo\Example;
class Example {
public function hello()
{
return "Hello, world!";
}
}
ServiceProvider.php
有关创建服务提供者的详细信息,请参阅Laravel文档。
<?php
namespace foo\Example;
class ServiceProvider extends \Illuminate\Support\ServiceProvider {
public function register()
{
$this->app->singleton('example', function($app) {
return new Example();
});
}
}
配置Slim
<?php require 'vendor/autoload.php'; use Slim\App; use Slim\Container; $app = new App(new Container()); $app->add(new \Germey\Emissary\Middleware([ 'foo\Example\ServiceProvider' ])); $app->get('/', function ($request, $response, $args) { $response->write($this->get('example')->hello()); return $response; }); $app->run();
当运行应用程序时,应从Example服务输出"Hello, world!"。
许可证
MIT许可证(MIT)
版权所有(c)2016 Michael Scott (https://github.com/Germey)
特此授予任何人无限制地使用本软件及其相关文档文件("软件")的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,以及允许向软件提供者提供软件的人进行此类操作,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按"原样"提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论这些责任是根据合同、侵权或其他法律而引起的,无论是由于软件或其使用或与其他软件或其使用相关。