laravel web.php 中 Route 为什么可以直接使用?
本文介绍了laravel web.php 中 Route 为什么可以直接使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
//比如
Route::get('/', 'HomeController@index');
解决方案
原理很简单
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
这个答案排版放不下了, 放到了文章里面
这篇关于laravel web.php 中 Route 为什么可以直接使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文