Laravel路由的$router和Route::的区别
原文:https://segmentfault.com/q/1010000004163673
问:
看别人的代码里好像很多用$router->的,看手册里好像都是Route::,这俩方式有什么区别吗?
答:Route::get
这种写法在Laravel
里叫做Facades
,本义是为了写起来方便而造出来的。
它等价于$app->make('router')->get
;
也是从服务容器里取出来的,提供路由服务的是 App\Providers\RouteServiceProvider
。
如果你通过依赖注入里拿到$router
那么,它也等价于$app->make('router')
,也等价于Route::
这个Facades
你可以参考一下Facades的文档理解一下:
https://laravel.com/docs/5.7/facades