laravel5.4 服务提供者设置
来源:互联网 发布:出纳风险评估矩阵图 编辑:程序博客网 时间:2024/06/11 08:54
进行编辑laravel时,需要进行编辑的是App文件夹,在这里我们需要的是两个文件夹Contracts,该文件夹用于存放接口文件,Services中是服务文件,我们首先在Contracts中建立TestContract.php文件内容如下:
<?phpnamespace App\Contracts;interface TestContract{public function callMe($controller);}
在Services当中建立TestServce.php文件
<?phpnamespace App\Services;use App\Contracts\TestContract;class TestService implements TestContract {public function callMe($controller){// TODO: Implement callMe() method.echo "This is called by ".$controller;}}
在php终端中输入
php artisan make:provider TestServiceProvider然后编辑 register函数
<?phpnamespace App\Providers;use App\Services\TestService;use Illuminate\Support\ServiceProvider;class TestServiceProvider extends ServiceProvider{ /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { //绑定单例$this->app->singleton("test", function($app){return new TestService();});//使用接口进行绑定$this->app->bind('App\Contracts\TestContract', function(){return new TestService();}); }}
所以会有两种方式进行获得实例化,一个是App::make("test")获得TestService类的对象,另一种则是通过接口App\Contracts\TestContract获得TestService类的对象
这样就OK了,接下来就做的是创建一个controller
php artisan make:controller TestController
接下来看下TestController
<?phpnamespace App\Http\Controllers;use App\Contracts\TestContract;use Illuminate\Http\Request;class TestController extends Controller{ //public function __construct(TestContract $test) {$this->test = $test;}public function index(){//$test = \App::make("test");$this->test->callMe("test");}}
这样就通过服务提供者调用了TestService!
阅读全文
0 0
- laravel5.4 服务提供者设置
- Laravel5.1中加入手机号重置密码模块----------ICO容器,Facade,服务提供者。
- dubbo 4 服务消费者到提供者过程
- 服务提供者框架
- 服务提供者框架
- 服务提供者框架
- 分层服务提供者(LSP)
- java 服务提供者框架
- Laravel服务提供者
- laravel-服务提供者
- 服务提供者框架示例
- 服务提供者框架模式
- Service Providers 服务提供者
- dubbo 服务提供者初始
- Spring Cloud 服务提供者
- 封装服务提供者框架
- 服务提供者框架
- 八、注册服务提供者
- 启动程序时提示缺少mfc.dll
- JavaScript笔记
- 51nod1432-贪心-独木舟
- redis在spring下的应用
- Ball Aizu
- laravel5.4 服务提供者设置
- 利用 CSS3 实现一个转动立体六边形 <一>
- 简单几步,提升.Net Core的开发效率
- Git详解之二 Git基础
- 【Python】面向对象class
- SequoiaDB巨杉数据库深度整合Spark 2.0,国内首家Spark认证数据库
- Asp.Net MVC4通过id更新表单
- 解决接串口设备后鼠标乱飘
- 【Redis源码剖析】