laravel的服务注入新增service层,多方式

laravel的服务注入新增service层,多方式

  • 我们现在使用的PHP框架基本都是MVC模式,业务代码基本都写Controller中,如果出现业务比较多,那么controller中的代码会非常的繁杂,后期更新迭代起来比较麻烦

  • 在JAVA中一般都存在service,DAO层等,我们这里需要引入service来解决controller冗杂的问题,在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;
}

以上方式都能获取相同的效果,如下
在这里插入图片描述

服务注入

  • 新建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中绑定不同的实现服务
    }
}
  • 在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;

猜你喜欢

转载自blog.csdn.net/weixin_43674113/article/details/114999341