• laravel框架总结(六) -- 门面(facades)


    Facades 为应用程序的服务容器中可用的类提供了一个「静态」接口。
     
    Laravel 本身附带许多的 facades,甚至你可能在不知情的状况下已经在使用他们!
     
    xpower的静态接口(门面,facades)
     
    前提条件1:我们有一个已经绑定到服务容器的类(xpower)
    前提条件2:我们已经在服务提供者中注册这个类(服务容器中regisrer下的singleton方法)
     
    5.接下来创建xpower的facade,都是继承facade基类。必须实现getFacadeAccessor方法。返回了一个字符串,这个字符串其实就是服务提供者注册绑定单例(singleton方法)的一个名称。这个名称可以随便写,我们这里是xpower
    <?php 

    namespace AppFacades;

    use IlluminateSupportFacadesFacade;

    class XP extends Facade {
      protected static function getFacadeAccessor() {
        return 'xpower';
      }
    }
    6.再然后需要到配置文件config/app.php中注册门面类别名:
    'aliases' => [ ...//其他门面类别名映射 'XP' => AppFacadesXP::class, ],
    我们此时可以直接使用XP::activate来激活xpower超能力
    use AppFacadesXP 的引入。不然找不到。
     
    分析:
    1.XP::activate先去容器中找这个XP的实例(已经在容器中注册),返回xpower字符串
    2.xpower这个字符串也已经绑定注册到容器中,然后返回Xpower服务实例
    3. ::activate这个其实会调用基类facade中的__callstatic方法,然后转换为->show()这个方式的调用。
  • 相关阅读:
    【codeforces 791D】 Bear and Tree Jumps
    【codeforces 791C】Bear and Different Names
    【codeforces 791B】Bear and Friendship Condition
    【codeforces 791A】Bear and Big Brother
    【t017】YL杯超级篮球赛
    Java Web整合开发(80) -- EJB & WebService
    搜索与排序
    T2821 天使之城 codevs
    T1155 金明的预算方案 codevs
    后缀表达式
  • 原文地址:https://www.cnblogs.com/ghjbk/p/6638114.html
Copyright © 2020-2023  润新知