摘要:我們知道,在中使用的話,只需要綁定模型,在創(chuàng)建表單,鏈接時(shí),直接可以拿來用,不需要多帶帶的去給路由別名如創(chuàng)建鏈接但是我們使用時(shí),在創(chuàng)建鏈接,嘗試用以上方法訪問時(shí),就會(huì)報(bào)錯(cuò)如創(chuàng)建鏈接拋出路由不存在的錯(cuò)誤那我們?nèi)绾蜗袷褂靡粯臃奖愕膩硎褂媚睾芎唵危?/p>
我們知道,在 laravel 中使用 resource 的話,只需要綁定模型,在創(chuàng)建表單,鏈接時(shí),直接可以拿來用,不需要多帶帶的去給路由 as 別名
如
Route::resource("main","MainController"); // 創(chuàng)建鏈接 URL::route("main.index")
但是我們使用 Route::controller 時(shí),在創(chuàng)建鏈接,嘗試用以上方法訪問時(shí),就會(huì)報(bào)錯(cuò)
如
Route::controller("main","MainController"); // 創(chuàng)建鏈接 URL::route("main.index") // 拋出路由不存在的錯(cuò)誤
那我們?nèi)绾蜗袷褂?resource 一樣方便的來使用 controller 呢?
很簡單,我們打開 controller 的源碼一看就知道了
// 源碼路徑:vendor/laravel/framework/src/Illuminate/Routing/Router.php :257 行 看到如下方法 /** * Route a controller to a URI with wildcard routing. * * @param string $uri * @param string $controller * @param array $names * @return void */ public function controller($uri, $controller, $names = array()) { $prepended = $controller; // First, we will check to see if a controller prefix has been registered in // the route group. If it has, we will need to prefix it before trying to // reflect into the class instance and pull out the method for routing. if ( ! empty($this->groupStack)) { $prepended = $this->prependGroupUses($controller); } $routable = $this->getInspector()->getRoutable($prepended, $uri); // When a controller is routed using this method, we use Reflection to parse // out all of the routable methods for the controller, then register each // route explicitly for the developers, so reverse routing is possible. foreach ($routable as $method => $routes) { foreach ($routes as $route) { $this->registerInspected($route, $controller, $method, $names); } } $this->addFallthroughRoute($controller, $uri); } // 我們看到可以傳遞第三個(gè)參數(shù),是一個(gè)數(shù)組,那么數(shù)組的內(nèi)容是什么呢?此方法里面沒有處理 name,我們注意看這一行 $this->registerInspected($route, $controller, $method, $names); //好了,我們找到 registerInspected 這個(gè)方法,看他如何處理 name protected function registerInspected($route, $controller, $method, &$names) { $action = array("uses" => $controller."@".$method); // If a given controller method has been named, we will assign the name to the // controller action array, which provides for a short-cut to method naming // so you don"t have to define an individual route for these controllers. $action["as"] = array_get($names, $method); $this->{$route["verb"]}($route["uri"], $action); }
我們看到他以 . 去切割了 name ,然后加入了進(jìn)去,這樣我們就清楚很多啦
路由這樣寫
Route::controller( "options", "OptionsController", [ "getSite"=>"options.site" ] ); // 現(xiàn)在就可以使用創(chuàng)建鏈接啦 URL::route("options.site")
這些東西找了下 laravel 文檔沒找著,所以自己直接看的源碼
本文發(fā)布源在:laravel Route::controller 使用路由命名
歡迎大家加入 laravel 交流群一起討論:365969825
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/20924.html
摘要:入口啟動(dòng)后,會(huì)先加載服務(wù)提供者中間件等組件,在查找路由之前因?yàn)槲覀兪褂玫氖情T面,所以先要查到的實(shí)體類。注冊第一步當(dāng)然還是通過服務(wù)提供者,因?yàn)檫@是啟動(dòng)的關(guān)鍵,在內(nèi)加載路由文件。因路由文件中沒有命名空間。 showImg(https://segmentfault.com/img/bVbhjvY?w=600&h=296); 前言 我的解析文章并非深層次多領(lǐng)域的解析攻略。但是參考著開發(fā)文檔看此...
摘要:簡介是一套簡介,優(yōu)雅開發(fā)框架,通過簡單,高雅,表達(dá)式語法開發(fā)應(yīng)用。服務(wù)器需要有該目錄及所有子目錄的寫入權(quán)限可用于存儲(chǔ)應(yīng)用程序所需的一些文件該目錄下包括緩存和編譯后的視圖文件日志目錄測試目錄該目錄下包含源代碼和第三方依賴包環(huán)境配置文件。 簡介 Laravel是一套簡介,優(yōu)雅PHP Web開發(fā)框架(PHP Web Framework), 通過簡單,高雅,表達(dá)式語法開發(fā)Web應(yīng)用。 特點(diǎn): ...
摘要:是用戶自定義函數(shù)中支持可變數(shù)量的參數(shù)列表。在及更早版本中,使用函數(shù),,和??勺償?shù)量的參數(shù)列表,這個(gè)概念可能你會(huì)覺得很抽象。我們再看一個(gè)示例以上例程會(huì)輸出可變數(shù)量參數(shù)將被傳遞到中,給定的數(shù)組會(huì)作為參數(shù)變量。 最近在讀 Laravel 源碼的時(shí)候,發(fā)現(xiàn)了一個(gè)段特別有趣的代碼,大家請(qǐng)看: showImg(https://segmentfault.com/img/remote/14600000...
摘要:下面是剛才說的這些步驟對(duì)應(yīng)的核心代碼收集路由和控制器里應(yīng)用的中間件我們在前面的文章里已經(jīng)詳細(xì)的解釋過中間件和路由的原理了,接下來就看看當(dāng)請(qǐng)求最終找到了路由對(duì)應(yīng)的控制器方法后是如何為控制器方法注入正確的參數(shù)并調(diào)用控制器方法的。 控制器 控制器能夠?qū)⑾嚓P(guān)的請(qǐng)求處理邏輯組成一個(gè)單獨(dú)的類, 通過前面的路由和中間件兩個(gè)章節(jié)我們多次強(qiáng)調(diào)Laravel應(yīng)用的請(qǐng)求在進(jìn)入應(yīng)用后首現(xiàn)會(huì)通過Http Ker...
摘要:本文主要學(xué)習(xí)總結(jié)下間參數(shù)傳遞。開發(fā)時(shí)經(jīng)常碰到類似場景有時(shí)需要在中讀取中設(shè)置的和,有時(shí)也需要在中讀取中設(shè)置的參數(shù)??偨Y(jié)下這幾個(gè)知識(shí)點(diǎn),便于查閱。 本文主要學(xué)習(xí)總結(jié)下Route,Middleware,Controller間參數(shù)傳遞。開發(fā)時(shí)經(jīng)常碰到類似場景:有時(shí)需要在Middleware中讀取Route中設(shè)置的middleware parameter和route parameter,有時(shí)也需...
閱讀 3961·2021-11-17 09:33
閱讀 3299·2021-10-08 10:05
閱讀 3126·2021-09-22 15:36
閱讀 1156·2021-09-06 15:02
閱讀 2782·2019-08-29 12:45
閱讀 1607·2019-08-26 13:40
閱讀 3414·2019-08-26 13:37
閱讀 436·2019-08-26 13:37