在laravel中,make方法用于从容器当中解析一个type,该type是源码当中定义的,解析后返回的结果就是type的一个实例,容器类调用make方法时,若没有已注册的key,会自动通过反射类实例化具体类。
本文操作环境:Windows10系统、Laravel6版、Dell G3电脑。
laravel中make方法的作用是什么
Laravel中的make方法是用来从容器当中解析一个type,这个type是源码当中定义的,不是很好翻译成中文。解析后返回的结果就是type的一个实例。
看过源码的同学应该知道在Illuminate\Foundation\Application这个类和它的父类Illuminate\Container\Container类中都有make方法,那么当执行如index.php中的这行代码,
1 $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
的时候,就会首先去执行Illuminate\Foundation\Application中的make方法,那么我们就先看看它。(这篇文章就以make这个Kernel类为例)
/** * Resolve the given type from the container. 从容器当中解析给定的type * * (Overriding Container::make) 覆盖了父类中的make方法 * * @param string $abstract 给定的type * @param array $parameters 指定一些参数 可选项 * @return mixed */ public function make($abstract, array $parameters = []) { $abstract = $this->getAlias($abstract);//调用父类中的getAlias方法 //如果在deferredServices这个数组设置了这个type并且在instances数组中没有设置这个type if (isset($this->deferredServices[$abstract]) && ! isset($this->instances[$abstract])) { $this->loadDeferredProvider($abstract);//那么就执行这个方法:加载被定义为延迟的服务提供者 } return parent::make($abstract, $parameters);//调用父类的make方法 }
好,我们一步一步的来,先看看这个getAlias方法,这个方法的作用就是返回这个类的别名,如果给出的是一个完整的类名且在aliases中已经设置了那么就返回这个类名的别名,如果没有设置过就返回这个类名本身,大家在看这个方法的时候可以先var_dump一下$app,对照着看里面的aliases数组,框架作者写这个方法真的很巧妙,至少这种递归方式在我实际开发当中很少用到。
/** * Get the alias for an abstract if available. * * @param string $abstract * @return string * * @throws \LogicException */ public function getAlias($abstract) { if (! isset($this->aliases[$abstract])) { return $abstract; } if ($this->aliases[$abstract] === $abstract) { throw new LogicException("[{$abstract}] is aliased to itself."); } return $this->getAlias($this->aliases[$abstract]); }
接下来就是对deferredServices和instances这个两个数组进行判断,在本例 $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); 当中,判断的结果为false,因此不执行loadDeferredProvider方法。
再接下来就是调用父类Illuminate\Container\Container中的make方法了,
/** * Resolve the given type from the container. * * @param string $abstract * @param array $parameters * @return mixed */ public function make($abstract, array $parameters = []) { return $this->resolve($abstract, $parameters);//直接调用resolve方法 }
【相关推荐:laravel视频教程】
以上是laravel中make方法的作用是什么的详细内容。更多信息请关注PHP中文网其他相关文章!

迁移在Laravelmanagedatabaseschema中,同时shandledatainterAction.1)迁移术语,允许创造,修改,且deletionoftables.2)models representDataAndDataAndDataAndDataAndDataAndDataAndDataAndDataAndProvideAnInterForceNtactaction,EnablingCrudoPerations

SoftDeletsinlaraveRareBetterTernaverainteraldatialdataAndRecoverability,而骨质骨骼验证了forderableford.fordableablefordataminimization和Privacy.1)softerdeleteseThesoftDeletDeletEstrait,允许restrestoratorralityandaudtrails和mayincroredatabasesize.2)物理

softDeletsinlesinlaravelareafareafareathataLowSyOutOmarkRecordSdeletedwithOutreMovingThemfromthedataBase.toimplementsoftesoftdeletes:1)addtheSoftDeletDeletEstraittRaittRaiteRemodoyourModeNClandInClandEnCludEthedEtelet_Atcolundelet_atcolumn.2)

laravelmigrationsareefectectivectiveDueTotheirversionControlandRoranderibalsible,slepliningDatabasemagementInwebDevelopment.1)heSpapsulatesCheMachangeNphpClasses,允许easyerollbacks

LaravelMigrationsareArareBestWhenFollowingTheSepractices:1)用户清除,描述性formigrations,例如'addemailtouserstable'.2)ensuremigrationsareReereSareReverSiblewitha'down'method.3)考虑到the the the the the the the the the the the the the the broaderimptactondataintegnegrityAndegrityAndegrinegrityAndertality.4)optimizeperformanceb

使用Laravel和Vue.js可以构建单页面应用(SPA)。1)在Laravel中定义API路由和控制器,处理数据逻辑。2)在Vue.js中创建组件化前端,实现用户界面和数据交互。3)配置CORS和使用axios进行数据交互。4)利用VueRouter实现路由管理,提升用户体验。

在Laravel中创建自定义辅助函数的步骤是:1.在composer.json中添加自动加载配置;2.运行composerdump-autoload更新自动加载器;3.在app/Helpers目录下创建并定义函数。这些函数能简化代码,提高可读性和可维护性,但需注意命名冲突和测试性。

在Laravel中处理数据库事务时,应使用DB::transaction方法,并注意以下要点:1.使用lockForUpdate()锁定记录;2.通过try-catch块处理异常,并在需要时手动回滚或提交事务;3.考虑事务的性能,缩短执行时间;4.避免死锁,可使用attempts参数重试事务。这段摘要完整地概括了如何在Laravel中优雅地处理事务,并提炼了文章中的核心要点和最佳实践。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用