Facades(一种设计模式,通常翻译为外观模式)提供了一个"static"(静态)接口去访问注册到IoC 容器中的类.
你的 facade 类只需要实现一个方法: getFacadeAccesor,该方法的工作是返回绑定到IoC的名字。例如:下面返回的就是cache
$value = Cache::get('key');
//Illuminate\Support\Facades\Cache类
class Cache extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'cache'; }
}所以,当用户引用任何在
Cache facade 中的静态方法, Laravel 从 IoC 容器绑定中取得
cache,并且执行请求的对象方法(get)
- 创建类
- 自动加载类
- 创建Facade(其实也是一个类) 此为门店模式
- 创建服务提供器(serviceProvideer)
- 加载服务提供器
(1) 在本地文件中创建 larval/app/PaymentGateway/Payment.php
namespace PaymentGateway;
class Payment {
public function process()
{
echo 'hello world ' ;
}
}
<span style="color:#FF0000;">(2) 自动加载类 coposer.json 中添加自动加载目录</span><pre name="code" class="php">"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/libraries/class",
"app/PaymentGateway" // 此为自动加载的目录
],
(3)创建Facade(其实也是一个类)创建门面(此可以返回一个payment实例)
在本地文件中创建 larval/app/PaymentGateway/PayFacade.php
namespace PaymentGateway;
use Illuminate\Support\Facades\Facade;
class PayFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'Payment';
}
}
(4)创建服务提供器(serviceProvideer)
larval/app/PaymentGateway/PaymentServiceProvider.php
创建方式1:
namespace PaymentGateway;
use Illuminate\Support\ServiceProvider;
class PaymentServiceProvider extends ServiceProvider
{
// 所谓的注册其实类似于一个用户在一个网站上注册.之后就是这个网站的用户了,然后下次就可以享受会员的一些优惠,
这里就是把某个类存放在ioc容器中,然后可以直接使用这个类的一些方法了; 所谓的服务提供器也就是类似于网站的注册页面,提供类到ioc的桥梁
public function register()
{
$this->app['Payment'] = $this->app->share(
function ($app) {
return new \PaymentGateway\Payment();
}
);
$this->app->booting(
function () {
$aliases = \Config::get('app.aliases');
if(empty($aliases['PayFacade'])){
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Payment','PaymentGateway\PayFacade');
}
}
);
}
}
创建方式1:
通过官方的方式要添加别名 在app/config/app.php里面添加别名
namespace PaymentGateway;
use Illuminate\Support\ServiceProvider;
class PaymentServiceProvider extends ServiceProvider
{
// 注册一个服务,其实就是把一个类放入ioc中
public function register()
{
$this->app->bindShared('Payment', function($app)
{
return new \PaymentGateway\Payment();
});
}
}
(5) 加载服务提供器
Laravel app/config/app.php 配置文件来加载该ServiceProvide
providers' => array(
'PaymentGateway\PaymentServiceProvider', //自动加载注册服务器
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
'Illuminate\Auth\AuthServiceProvider',
'Illuminate\Cache\CacheServiceProvider',
'Illuminate\Session\CommandsServiceProvider',
)
(二) 你也可以使用instance方法,将一个已经存在的对象接口绑定到容器中:
(1) 绑定实例
在laravel\bootstrap\start.php 文件最下面 添加
$myInstance = new \PaymentGateway\Payment();
$app->instance('test', $myInstance);
(2) 提取实例:
在Controller或者router中提取实例
$value = App::make('test'); // 输出hello world
$value->process();
本文介绍如何在Laravel框架中使用Facades(外观模式)和自定义服务提供者来封装类并进行依赖注入。包括创建自定义类、设置自动加载、构建Facades、注册服务提供者等步骤。
 laravel Facades&spm=1001.2101.3001.5002&articleId=45740567&d=1&t=3&u=1d63ce1f07f147469fd766e0eda8688e)
1万+

被折叠的 条评论
为什么被折叠?



