使用 Laravel 服务容器(框架的核心)的优势

2020-05-2215:05:42后端程序开发使用 Laravel 服务容器(框架的核心)的优势已关闭评论1,887 views字数 3123阅读模式

laravel框架的核心是什么,那么无疑是服务容器。理解服务容器的概念,对于我们使用laravel太重要了,应该说是否理解服务容器的概念是区分是否入门laravel的重要条件。因为整个框架正是在服务容器这一基础上构建起来的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

laravel服务容器就像一个高度自动化的工厂,你需要的东西,定制好模型,使用特定接口来制造。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

因为使用了服务容器,laravel中大部分对象实例化的方式是这样的:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$obj1 = $container->make('class1', 'class2');文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$obj2 = $container->make('class3', 'class4');文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

但是在没有使用服务容器的情况下,以下这种方式同样可以做到:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$obj1 = new class1(new class2());文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$obj2 = new class3(new class4());文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

那么使用服务容器的优势到底是什么呢?下面我们通过一些具体例子来分析下它的优势:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

例一、发送邮件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

我们把发送邮件的功能封装成一个类,需要使用的时候,实例化并调用发送方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

以下是不使用laravel服务容器常见的方式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

/**文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

*发送邮件服务类文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

*/文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

class EmailService{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

public function send(){文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//todo 发送邮件方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//如果任何地方要发邮件我们就复制下面这两行代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$emailService = new EmailService();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$emailService->send();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

使用了了laravel服务容器以后:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$this->app->bind('emailService', function ($app) {文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

return new EmailService();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

});文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//如果任何地方要发邮件我们就复制下面这两行代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$emailService = app('emailService');文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$emailService->send();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

这使得我们的代码更加简洁了,并且由于有了中间层,灵活性提高了(解耦),那么无论是测试(在测试时我们可以伪造类替换EmailService类)还是优化EmailService类,都变得更加方便。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//只需要改这一个地方文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$this->app->bind('emailService', function ($app) {文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

return new SupperEmailService();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

});文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

其他调用的部分我们完全不用动,如果我们没有这个绑定操作,那么不得不在每个使用邮件服务的地方做更改。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//使用到EamilSerice类的每个地方都要更改文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$emailService = new SupperEmailService();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$emailService->send();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

例二、实现单例模式文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

还是上面的例子,出于性能的考虑,你需要SupperEamilService类实现单例模式,于是在不使用laravel服务容器的情况下,你将SupperEmailService类更改如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

class SupperEamilService{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//创建静态私有的变量保存该类对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

static private $instance;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//防止直接创建对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

private function __construct(){文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//防止克隆对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

private function __clone(){文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

static public function getInstance(){文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//判断$instance是否是Uni的对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//没有则创建文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

if (!self::$instance instanceof self) {文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

self::$instance = new self();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

return self::$instance;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//发送邮件方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

public function send(){文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

除此之外,由于现在SupperEamilService类构造函数为私有,无法通过new关键字来实例化对象,所以在每个实例化SupperEmailService类的地方都要改成这样:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$emailService=SupperEmailService::getInstance();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$emailService->send();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

laravel服务容器天生支持单例,下面是laravel的实现方式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//只需要把bind改成singleton文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$this->app->singleton('emailService', function ($app) {文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

return new SupperEmailService();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

});文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

要实现单例甚至只需要改一行代码,把原来的bind方法改成singleton ,通过容器取出来的便是单例,真是太方便了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

例三、旅行者去旅行文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

这个例子假设一个旅行者去西藏旅行,可以做火车(train)或者走路(leg)去。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

不使用laravel服务容器:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

<?php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

interface TrafficTool文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

public function go();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

class Train implements TrafficTool文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

public function go()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

echo "train....";文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

class Leg implements TrafficTool文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

public function go()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

echo "leg..";文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

class Traveller文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

/**文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

* @var Leg|null|Train文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

* 旅行工具文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

*/文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

protected $_trafficTool;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

public function __construct(TrafficTool $trafficTool)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$this->_trafficTool = $trafficTool;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

public function visitTibet()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$this->_trafficTool->go();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

当旅行者要坐火车去旅行通常我们这样写:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

<?php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$train = new Train();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$tra = new Traveller($train);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$tra->visitTibet();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

事实上这种写法已经非常不错了,因为对于旅行工具的依赖已经通过接口的方式转移到外部了。但是使用new来实例化对象的时候还是会产生依赖.比如上面trafficTool),这说明我们要创建一个Traveller之前必须得有一个$trafficTool,即Traveller依赖于trafficTool.当使用new来实例化Traveller的时候,Traveller和trafficTool之间就产生了耦合.这样,这两个组件就没办法分开了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

现在我们来看看使用laravel服务容器是怎么实现的:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

在服务容器中绑定类文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

<?php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

namespace App\Providers;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

class RepositoryServiceProvider extends ServiceProvider文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

public function register()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

//在服务容器中绑定类文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$this->app->bind( 'TrafficTool', 'Train');文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$this->app->bind('Traveller', 'Traveller');文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

实例化对象
<?php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

// 实例化对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$tra = app()->make('Traveller');文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

$tra->visitTibet();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

当我们使用服务容器获取旅行类的对象时,容器会自动注入对象所需要的参数。而在此之前我只需要绑定特定的类就可以了,这样做才体现了真正的自动化,而且使得旅行类和旅行工具类完全解耦了。当我们需要更改旅行方式的时候,我们就只需要更改绑定就可以了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19259.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/19259.html