1.定义接口TestContract
<?php
namespace App\Contract;
interface TestContract
{
public function test($msg='');
}
2.实现接口TestContract
<?php
namespace App\Contract;
class Test implements TestContract
{
public function test($msg=''){
echo 'I am Test~ '.$msg;
}
}
?>
<?php
namespace App\Contract;
use Illuminate\Support\ServiceProvider;
use App\Contract\Test;
class TestServiceProvider extends ServiceProvider
{
public function register(){
// 绑定test与Test类的实例
$this->app->singleton('test', function($app){
return new Test();
});
}
}
?>
<?php
namespace App\Contract;
use Illuminate\Support\Facades\Facade;
class TestFacade extends Facade
{
// 这里的test跟服务提供者TestServiceProvider里面注册的'test'一致
protected static function getFacadeAccessor() { return 'test'; }
}
?>
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Test;
class TestController extends Controller {
public function test(){
Test::test('hello world');
}
}