laravel没有route.php,laravel web.php 中 Route 为什么可以直接使用?

原理很简单

1 . 首先, 你注意一下 /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' => [

'Route' => Illuminate\Support\Facades\Route::class,

];

2 . 因为有 Facades, 所以我们直接去看 Illuminate\Support\Facades\Route::class 这个类返回的内容

* @method static \Illuminate\Routing\Route get(string $uri, \Closure|array|string $action)

/**

* Get the registered name of the component.

*

* @return string

*/

protected static function getFacadeAccessor()

{

return 'router';

}

3 . 那就简单了, 直接去找注册为 router 的组件

发现是在 Illuminate/Routing/RoutingServiceProvider.php

/**

* Register the router instance.

*

* @return void

*/

protected function registerRouter()

{

$this->app->singleton('router', function ($app) {

return new Router($app['events'], $app);

});

}

4 . new Router() 看到了没, 很显然就会返回 Illuminate/Routing/Router.php 实例; 是不是发现了

public function get($uri, $action = null)

{

return $this->addRoute(['GET', 'HEAD'], $uri, $action);

}

(づ ̄3 ̄)づ╭❤~ 请宣我 !! 么么

问答时间

我们可以确认了 'router' 是在

Illuminate/Routing/RoutingServiceProvider.php 里面的

但是为什么没有配置在 /config/app.php 的 providers 里面呢

答案在这里

Illuminate/Foundation/Application.php

注意 base service providers 和 configured providers

这个答案排版放不下了, 放到了文章里面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值