0
点赞
收藏
分享

微信扫一扫

Laravel 三种注入方式


阅读目录

  • 业务场景
  • 方法注入
  • 构造注入
  • 以上方式获取相同的效果,如下
  • 服务注入

业务场景

一个网站首页的展示模块很多,在 controller 中写入所有的业务代码会导致非常的长,修改起来也非常难整,有些部分具有重用性,直接写导致系统冗余,这里简单以查询文章模块为例新建 serveice 进行注入到 controller

在 controller 查询文章列表直接写法,这里是简单实例没有复杂的业务,只是进行单纯的查询,实际业务可能是复杂的。

public function index()
{
	//....假设这里面很多查询文章的业务
    $articles = Article::select('book_id','pid','title')->get();
    return $articles;
}

我们把这一段进行提取到 service 层再注入到 controller 中进行调用,
我们新建文件:app\Services\Article\Services\ArticleService.php

<?php

namespace App\Services\Article\Services;

use App\Models\Article;

class ArticleService
{

    public function getArticle()
    {
        return Article::select('book_id', 'pid', 'title')->get();
    }

}

方法注入

需要 use 引入相应依赖。

public function index(ArticleService $articleService)
{
	$articles = $articleService->getArticle();
	return $articles;
}

构造注入

需要 use 引入相应依赖。

protected $articleService;

public function __construct(ArticleService $articleService)
{
    $this->articleService = $articleService;
}

public function index()
{
    $articles = $this->articleService->getArticle();
    return $articles;
}

以上方式获取相同的效果,如下

Laravel 三种注入方式_开发语言

服务注入

新建 interface 接口:
app\Services\Article\interfaces\ArticleServiceInterface.php

实现这个接口: ArticleService.php

<?php

namespace App\Services\Article\Services;

use App\Models\Article;
use App\Services\Article\Interfaces\ArticleServiceInterface;

class ArticleService implements ArticleServiceInterface
{

    public function getArticle()
    {
        return Article::select('book_id', 'pid', 'title')->get();
    }

}

新建服务 Provider

app\Services\Article\Providers\ArticleServiceProvider.php

<?php

namespace App\Services\Article\Providers;

use App\Services\Article\Interfaces\ArticleServiceInterface;
use App\Services\Article\Services\ArticleService;
use Illuminate\Support\ServiceProvider;

class ArticleServiceProvider extends ServiceProvider
{
    public function boot()
    {
        //
    }
    
    public function register()
    {
        $this->app->instance(ArticleServiceInterface::class,$this->app->make(ArticleService::class));
        //这里可以注册多个服务,make中绑定不同的实现服务
    }
}

\app\Providers\AppServiceProvider.php 中注册服务

<?php

namespace App\Providers;

use App\Services\Article\Providers\ArticleServiceProvider;
use App\Services\Book\Interfaces\BookServiceInterface;
use App\Services\Book\Services\BookService;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    
    public function boot()
    {
        //
    }

   
    public function register()
    {
        $this->app->register(ArticleServiceProvider::class);
    }
}

controller 中进行使用,这里我们构造引入接口 Interface ,在服务中我们可以绑定实际使用的类,灵活度加强。

protected $articleService;

public function __construct(ArticleServiceInterface $articleService)
{
    $this->articleService = $articleService;
}


public function index()
{

    $articles = $this->articleService->getArticle();
    return $articles;
}


举报

相关推荐

0 条评论