日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

laravel的服務(wù)注入新增service層,多方式

作者:渡目成書 更新時間: 2022-01-27 編程語言

laravel的服務(wù)注入新增service層,多方式

  • 我們現(xiàn)在使用的PHP框架基本都是MVC模式,業(yè)務(wù)代碼基本都寫Controller中,如果出現(xiàn)業(yè)務(wù)比較多,那么controller中的代碼會非常的繁雜,后期更新迭代起來比較麻煩

  • 在JAVA中一般都存在service,DAO層等,我們這里需要引入service來解決controller冗雜的問題,在laravel可以通過一般直接注入和服務(wù)來解決。

  • 例:業(yè)務(wù)場景,一個網(wǎng)站首頁的展示模塊很多,在controller中寫入所有的業(yè)務(wù)代碼會導(dǎo)致非常的長,修改起來也非常難整,有些部分具有重用性,直接寫導(dǎo)致系統(tǒng)冗余,這里簡單以查詢文章模塊為例新建serveice進行注入到controller

直接注入

  • 在controller查詢文章列表直接寫法,這里是簡單實例沒有復(fù)雜的業(yè)務(wù),只是進行單純的查詢,實際業(yè)務(wù)可能是復(fù)雜的。
public function index()
    {
		//....假設(shè)這里面很多查詢文章的業(yè)務(wù)
        $articles = Article::select('book_id','pid','title')->get();
        return $articles;
}
  • 我們把這一段進行提取到service層再注入到controller中進行調(diào)用,我們新建文件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引入相應(yīng)依賴

 public function index(ArticleService $articleService)
    {

        $articles = $articleService->getArticle();

        return $articles;
        }

方式二(構(gòu)造注入)

需要use引入相應(yīng)依賴

protected $articleService;

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


    public function index()
    {

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

        return $articles;
}

以上方式都能獲取相同的效果,如下
在這里插入圖片描述

服務(wù)注入

  • 新建interface接口,app\Services\Article\interfaces\ArticleServiceInterface.php,并將ArticleService.php實現(xiàn)這個接口
<?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();
    }

}
  • 新建服務(wù)Provider,app\Services\Article\Providers\ArticleServiceProvider.php,并注冊我們的接口服務(wù)
<?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));
        //這里可以注冊多個服務(wù),make中綁定不同的實現(xiàn)服務(wù)
    }
}
  • 在AppServiceProvider.php中注冊我們的服務(wù)
 <?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中進行使用,這里我們構(gòu)造引入是接口Interface,在服務(wù)中我們可以綁定實際使用的類,靈活度加強
protected $articleService;

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


    public function index()
    {

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

        return $articles;

原文鏈接:https://blog.csdn.net/weixin_43674113/article/details/114999341

欄目分類
最近更新