标签:hello style ret ide 案例 ice log his return
1.定义服务:对某个业务进行逻辑封装之后的一个类
<?php namespace App\Services; class TestService { public function __construct() { } public function helloWorld() { echo ‘hello world‘; } }
2.定义服务提供者:需要将定义好的服务类注册绑定,以便在程序中使用
<?php namespace App\Providers; use App\Services\TestService; use Illuminate\Support\ServiceProvider; class TestServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } public function register() { $this->app->bind(‘test‘, function ($app) { return new TestService(); }); } }
3.注册服务提供者到容器:
App\Providers\TestServiceProvider::class,
4.使用我们的服务
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; /** * */ class TestController extends Controller { public function test1() { app(‘test‘)->helloWorld(); } }
标签:hello style ret ide 案例 ice log his return
原文地址:http://www.cnblogs.com/lv-lu/p/6385644.html