发布于2021-03-14 06:08 阅读(1487) 评论(0) 点赞(16) 收藏(4)
Contracts
Contracts其实就是倡导面向接口编程,来达到解耦的目的。而这些通用的接口已经由Laravel为你设计好了。就是这些Contracts.
那么Laravel如何知道我们需要使用哪个实现呢?
在Laravel默认的Contracts绑定中,在'Illuminate/Foundation/Application.php'有这样的定义:这就是绑定了默认的接口实现.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | /** * 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中使用进行绑定:
1 | $ this ->app->bind( 'App\Contracts\EventPusher' , 'App\Services\PusherEventPusher' ); |
Facades
Facades 为应用程序的服务容器中可用的类提供了一个「静态」接口。Laravel 「facades」作为在服务容器内基类的「静态代理」。很难懂?
我们打开项目目录下的config/app.php,然后找到
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | /* |-------------------------------------------------------------------------- | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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
1 2 3 4 5 6 7 8 9 10 11 | <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?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.
1 2 3 | Route:: get ( '/facadeex' , function(){ return TestFacades::testingFacades(); }); |
Step 9 − Visit the following URL to test the Facade.
http://localhost:8000/facadeex去查看输出
更多学习内容请访问:
腾讯T3-T4标准精品PHP架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)
原文链接:https://www.cnblogs.com/a609251438/p/12707971.html
作者:你是好人
链接:http://www.phpheidong.com/blog/article/3603/97b0fce7fed011cb8c09/
来源:php黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 php黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-4
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!