首页 > 其他 > 详细

服务提供者案例

时间:2017-02-10 12:59:17      阅读:264      评论:0      收藏:0      [点我收藏+]

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();
    }
}

 

服务提供者案例

原文:http://www.cnblogs.com/lv-lu/p/6385644.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!