contracts

 

contracts其实就是倡导面向接口编程,来达到解耦的目的。而这些通用的接口已经由laravel为你设计好了。就是这些contracts.

那么laravel如何知道我们需要使用哪个实现呢?

在laravel默认的contracts绑定中,在’illuminate/foundation/application.php’有这样的定义:这就是绑定了默认的接口实现.

/**

     * register the core class aliases in the container.

     *

     * @return void

     */

    public function registercorecontaineraliases()

    {

        $aliases = [

            'app'                  => ['illuminate\foundation\application', 'illuminate\contracts\container\container', 'illuminate\contracts\foundation\application'],

            'auth'                 => 'illuminate\auth\authmanager',

            'auth.driver'          => ['illuminate\auth\guard', 'illuminate\contracts\auth\guard'],

            'auth.password.tokens' => 'illuminate\auth\passwords\tokenrepositoryinterface',

            'blade.compiler'       => 'illuminate\view\compilers\bladecompiler',

            'cache'                => ['illuminate\cache\cachemanager', 'illuminate\contracts\cache\factory'],

            'cache.store'          => ['illuminate\cache\repository', 'illuminate\contracts\cache\repository'],

            'config'               => ['illuminate\config\repository', 'illuminate\contracts\config\repository'],

            'cookie'               => ['illuminate\cookie\cookiejar', 'illuminate\contracts\cookie\factory', 'illuminate\contracts\cookie\queueingfactory'],

            'encrypter'            => ['illuminate\encryption\encrypter', 'illuminate\contracts\encryption\encrypter'],

            'db'                   => 'illuminate\database\databasemanager',

            'db.connection'        => ['illuminate\database\connection', 'illuminate\database\connectioninterface'],

            'events'               => ['illuminate\events\dispatcher', 'illuminate\contracts\events\dispatcher'],

            'files'                => 'illuminate\filesystem\filesystem',

            'filesystem'           => ['illuminate\filesystem\filesystemmanager', 'illuminate\contracts\filesystem\factory'],

            'filesystem.disk'      => 'illuminate\contracts\filesystem\filesystem',

            'filesystem.cloud'     => 'illuminate\contracts\filesystem\cloud',

            'hash'                 => 'illuminate\contracts\hashing\hasher',

            'translator'           => ['illuminate\translation\translator', 'symfony\component\translation\translatorinterface'],

            'log'                  => ['illuminate\log\writer', 'illuminate\contracts\logging\log', 'psr\log\loggerinterface'],

            'mailer'               => ['illuminate\mail\mailer', 'illuminate\contracts\mail\mailer', 'illuminate\contracts\mail\mailqueue'],

            'auth.password'        => ['illuminate\auth\passwords\passwordbroker', 'illuminate\contracts\auth\passwordbroker'],

            'queue'                => ['illuminate\queue\queuemanager', 'illuminate\contracts\queue\factory', 'illuminate\contracts\queue\monitor'],

            'queue.connection'     => 'illuminate\contracts\queue\queue',

            'redirect'             => 'illuminate\routing\redirector',

            'redis'                => ['illuminate\redis\database', 'illuminate\contracts\redis\database'],

            'request'              => 'illuminate\http\request',

            'router'               => ['illuminate\routing\router', 'illuminate\contracts\routing\registrar'],

            'session'              => 'illuminate\session\sessionmanager',

            'session.store'        => ['illuminate\session\store', 'symfony\component\httpfoundation\session\sessioninterface'],

            'url'                  => ['illuminate\routing\urlgenerator', 'illuminate\contracts\routing\urlgenerator'],

            'validator'            => ['illuminate\validation\factory', 'illuminate\contracts\validation\factory'],

            'view'                 => ['illuminate\view\factory', 'illuminate\contracts\view\factory'],

        ];

  

在我们自定义的接口实现时,我们可以在serviceprovider中使用进行绑定:

$this->app->bind('app\contracts\eventpusher', 'app\services\pushereventpusher');

  

facades

facades 为应用程序的服务容器中可用的类提供了一个「静态」接口。laravel 「facades」作为在服务容器内基类的「静态代理」。很难懂?

我们打开项目目录下的config/app.php,然后找到

/*

    |--------------------------------------------------------------------------

    | class aliases

    |--------------------------------------------------------------------------

    |

    | this array of class aliases will be registered when this application

    | is started. however, feel free to register as many as you wish as

    | the aliases are "lazy" loaded so they don't hinder performance.

    |

    */

    'aliases' => [

        'app'       => illuminate\support\facades\app::class,

        'artisan'   => illuminate\support\facades\artisan::class,

        'auth'      => illuminate\support\facades\auth::class,

        'blade'     => illuminate\support\facades\blade::class,

        'bus'       => illuminate\support\facades\bus::class,

        'cache'     => illuminate\support\facades\cache::class,

        'config'    => illuminate\support\facades\config::class,

        'cookie'    => illuminate\support\facades\cookie::class,

        'crypt'     => illuminate\support\facades\crypt::class,

        'db'        => illuminate\support\facades\db::class,

        'eloquent'  => illuminate\database\eloquent\model::class,

        'event'     => illuminate\support\facades\event::class,

        'file'      => illuminate\support\facades\file::class,

        'gate'      => illuminate\support\facades\gate::class,

        'hash'      => illuminate\support\facades\hash::class,

        'input'     => illuminate\support\facades\input::class,

        'lang'      => illuminate\support\facades\lang::class,

        'log'       => illuminate\support\facades\log::class,

        'mail'      => illuminate\support\facades\mail::class,

        'password'  => illuminate\support\facades\password::class,

        'queue'     => illuminate\support\facades\queue::class,

        'redirect'  => illuminate\support\facades\redirect::class,

        'redis'     => illuminate\support\facades\redis::class,

        'request'   => illuminate\support\facades\request::class,

        'response'  => illuminate\support\facades\response::class,

        'route'     => illuminate\support\facades\route::class,

        'schema'    => illuminate\support\facades\schema::class,

        'session'   => illuminate\support\facades\session::class,

        'storage'   => illuminate\support\facades\storage::class,

        'url'       => illuminate\support\facades\url::class,

        'validator' => illuminate\support\facades\validator::class,

        'view'      => illuminate\support\facades\view::class,

    ],

  

你是不是发现了什么?对,facades其实就是在config/app.php中定义的一系列类的别名。只不过这些类都具有一个共同的特点,那就是继承基底 illuminate\support\facades\facade 类并实现一个方法:getfacadeaccessor返回名称。

自定义facade

参考http://www.tutorialspoint.com/laravel/laravel_facades.htm

step 1 −创建一个名为 testfacadesserviceprovider的serviceprovider ,使用如下命令即可:

php artisan make:provider testfacadesserviceprovider

step 2 − 创建一个底层代理类,命名为“testfacades.php” at “app/test”.

app/test/testfacades.php

<?php

namespace app\test;

class testfacades{

   public function testingfacades(){

      echo "testing the facades in laravel.";

   }

}

?>

  

step 3 − 创建一个 facade 类 called “testfacades.php” at “app/test/facades”.

app/test/facades/testfacades.php

<?php

namespace app\test\facades;

use illuminate\support\facades\facade;

class testfacades extends facade{

   protected static function getfacadeaccessor() { return 'test'; }

}

  

step 4 −创建一个serviceproviders类,名为“testfacadesserviceproviders.php” at “app/test/facades”.

app/providers/testfacadesserviceproviders.php

<?php
namespace app\providers;
use app;
use illuminate\support\serviceprovider;
class testfacadesserviceprovider extends serviceprovider {
 public function boot() {
 //
 }
 public function register() {
 //可以这么绑定,这需要use app;
 //  app::bind('test',function() {
 //     return new \app\test\testfacades;
 //  });
  
 //也可以这么绑定,推荐。这个test对应于facade的getfacadeaccessor返回值
 $this->app->bind("test", function(){
 return new myfoo(); //给这个facade返回一个代理实例。所有对facade的调用都会被转发到该类对象下。
 });
 }
}

  

step 5 − 在config/app.php注册serviceprovider类

step 6 − 在config/app.php注册自定义facade的别名

使用测试:

add the following lines in app/http/routes.php.

route::get('/facadeex', function(){
 return testfacades::testingfacades();
});

  

step 9 − visit the following url to test the facade.

http://localhost:8000/facadeex去查看输出

更多学习内容请访问:

腾讯t3-t4标准精品php架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)