這篇文章將為大家詳細(xì)講解有關(guān)Laravel中如何使用路由,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

入口
Laravel啟動(dòng)后,會(huì)先加載服務(wù)提供者、中間件等組件,在查找路由之前因?yàn)槲覀兪褂玫氖情T面,所以先要查到Route的實(shí)體類。
注冊(cè)
第一步當(dāng)然還是通過服務(wù)提供者,因?yàn)檫@是laravel啟動(dòng)的關(guān)鍵,在RouteServiceProvider內(nèi)加載路由文件。
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace) // 設(shè)置所處命名空間
->group(base_path('routes/api.php')); //所得路由文件絕對(duì)路徑
}首先require是不可缺少的。因路由文件中沒有命名空間。Illuminate\Routing\Router 下方法
protected function loadRoutes($routes)
{
if ($routes instanceof Closure) {
$routes($this);
} else {
$router = $this;
require $routes;
}
}隨后通過路由找到指定方法,依舊是 Illuminate\Routing\Router內(nèi)有你所使用的所有路由相關(guān)方法,例如get、post、put、patch等等,他們都調(diào)用了統(tǒng)一的方法addRoute
public function addRoute($methods, $uri, $action)
{
return $this->routes->add($this->createRoute($methods, $uri, $action));
}之后通過Illuminate\Routing\RouteCollectionaddToCollections 方法添加到集合中
protected function addToCollections($route)
{
$domainAndUri = $route->getDomain().$route->uri();
foreach ($route->methods() as $method) {
$this->routes[$method][$domainAndUri] = $route;
}
$this->allRoutes[$method.$domainAndUri] = $route;
}添加后的結(jié)果如下圖所示

實(shí)例化
依舊通過反射加載路由指定的控制器,這個(gè)時(shí)候build的參數(shù)$concrete =App\Api\Controllers\XxxController
public function build($concrete)
{
// If the concrete type is actually a Closure, we will just execute it and
// hand back the results of the functions, which allows functions to be
// used as resolvers for more fine-tuned resolution of these objects.
if ($concrete instanceof Closure) {
return $concrete($this, $this->getLastParameterOverride());
}
$reflector = new ReflectionClass($concrete);
// If the type is not instantiable, the developer is attempting to resolve
// an abstract type such as an Interface of Abstract Class and there is
// no binding registered for the abstractions so we need to bail out.
if (! $reflector->isInstantiable()) {
return $this->notInstantiable($concrete);
}
$this->buildStack[] = $concrete;
$constructor = $reflector->getConstructor();
// If there are no constructors, that means there are no dependencies then
// we can just resolve the instances of the objects right away, without
// resolving any other types or dependencies out of these containers.
if (is_null($constructor)) {
array_pop($this->buildStack);
return new $concrete;
}
$dependencies = $constructor->getParameters();
// Once we have all the constructor's parameters we can create each of the
// dependency instances and then use the reflection instances to make a
// new instance of this class, injecting the created dependencies in.
$instances = $this->resolveDependencies(
$dependencies
);
array_pop($this->buildStack);
return $reflector->newInstanceArgs($instances);
}這時(shí)將返回控制器的實(shí)例,下面將通過url訪問指定方法,一般控制器都會(huì)繼承父類Illuminate\Routing\Controller,laravel為其設(shè)置了別名 BaseController
public function dispatch(Route $route, $controller, $method)
{
$parameters = $this->resolveClassMethodDependencies(
$route->parametersWithoutNulls(), $controller, $method
);
if (method_exists($controller, 'callAction')) {
return $controller->callAction($method, $parameters);
}
return $controller->{$method}(...array_values($parameters));
}Laravel通過controller繼承的callAction去調(diào)用子類的指定方法,也就是我們希望調(diào)用的自定義方法。
public function callAction($method, $parameters)
{
return call_user_func_array([$this, $method], $parameters);
}關(guān)于Laravel中如何使用路由就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
本文標(biāo)題:Laravel中如何使用路由-創(chuàng)新互聯(lián)
鏈接地址:http://www.chinadenli.net/article18/dchigp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、企業(yè)網(wǎng)站制作、網(wǎng)站內(nèi)鏈、網(wǎng)站維護(hù)、企業(yè)建站、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容