contracts
contracts其实就是倡导面向接口编程,来达到解耦的目的。而这些通用的接口已经由laravel为你设计好了。就是这些contracts.
那么laravel如何知道我们需要使用哪个实现呢?
在laravel默认的contracts绑定中,在’illuminate/foundation/application.php’有这样的定义:这就是绑定了默认的接口实现.
/** * register the core class alias in the container. * * @return void */ public function registercorecontaineralias() { $alias = [ '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\databa\databamanager', 'db.connection' => ['illuminate\databa\connection', 'illuminate\databa\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' 2017春运开始时间=> ['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\databa', 'illuminate\contracts\redis\databa'], 'request' => 'illuminate\http\request', 'router' => ['illuminate\routing\router', 'illuminate\contracts\routing\registrar'], 'ssion' => 'illuminate\ssion\ssionmanager', 'ssion.store' => ['illuminate\ssion\store', 'symfony\component\httpfoundation\ssion\ssioninterface'], 'url' => ['illuminate\routing\urlgenerator', 'illuminate\contracts\routing\urlgenerator'], 'validator' => ['illuminate\validation\factory', 'illuminate\contracts\validation\factory'], 'view' => ['illuminate\view\factory', 'illuminate\contracts\view\factory'], ];
在我们自定义的接口实现时,我们可以在rviceprovider中使用进行绑定:
$this->app->bind('app\contracts\eventpusher', 'app\rvices\pushereventpusher');
facades
facades 为应用程序的服务容器中可用的类提供了一个「静态」接口。laravel 「facades」作为在服务容器内基类的「静态代理」。很难懂?
我们打开项目目录下的config/app.php,然后找到
/* |-------------------------------------------------------------------------- | class alias |-------------------------------------------------------------------------- | | this array of class alias will be registered when this application | is started. however, feel free to register as many as you wish as | the alias are "lazy" loaded so they don't hinder performance. | */ 'alias' => [ '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\databa\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, 'respon' => illuminate\support\facades\respon::class, 'route' => illuminate\support\facades\route::class, 'schema' => illuminate\support\facades\schema::class, 'ssion' => illuminate\support\facades\ssion::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−创建一个名为 testfacadesrviceprovider的rviceprovider ,使用如下命令即可:
php artisan make:provider testfacadesrviceprovider
step 2− 创建一个底层代理类,命名为“testfacades.php” at “app/test”.
app/test/testfacades.php
<?phpnamespace app\test;class testfacad白蚂蚁es{ public function testingfacades(){ echo "testing the facades in laravel."; }}?>
step 3− 创建一个 facade 类 called “testfacades.php” at “app/test/facades”.
app/test/facades/testfacades.php
<?phpnamespace app\test\facades;u illuminate\support\facades\facade;class testfacades extends facade{ protected s春天真美tatic function getfacadeaccessor() { return 'test'; }}
step 4−创建一个rviceproviders类,名为“testfacadesrviceproviders.ph世界沙漠p” at “app/test/facades”.
app/providers/testfacadesrviceproviders.php
<?phpnamespace app\providers;u app;u illuminate\support\rviceprovider;class testfacadesrviceprovider extends rviceprovider { public function boot() { // } public function register() { //可以这么绑定,这需要u 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注册rviceprovider类
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架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)
本文发布于:2023-04-08 12:14:09,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/b066303d0b37ba707a06ca9f9a7b35b6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Laravel之Contracts和Facades详解.doc
本文 PDF 下载地址:Laravel之Contracts和Facades详解.pdf
留言与评论(共有 0 条评论) |