门面模式(Facade) laravel Facades

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)

  1. 创建类
  2. 自动加载类
  3. 创建Facade(其实也是一个类) 此为门店模式
  4. 创建服务提供器(serviceProvideer)
  5. 加载服务提供器

(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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值