Home  >  Article  >  Backend Development  >  How to register a service provider using Illuminate for non-Laravel projects?!

How to register a service provider using Illuminate for non-Laravel projects?!

WBOY
WBOYOriginal
2016-08-04 09:20:381031browse

How to register a service provider using Illuminate for non-Laravel projects?!

Reply content:

How to register a service provider using Illuminate for non-Laravel projects?!

Find the class registered by ServiceProvider and instantiate this class directly for use.

You can refer to laravel’s implementation:

IlluminateFoundationApplication

<code>class Example
{
    public function say()
    {
        echo 'hello';
    }
}


$app = new Illuminate\Container\Container;
$app->instance('Illuminate\Container\Container',$app);  //共享容器对象

//注册一个服务
$app->bind('example',function(){
    return new Example();
});


$app->make('example')->say();       //hello


$app2 = new Illuminate\Container\Container; //获得共享的容器对象,即$app

$app2->make('example')->say();      //hello
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn