摘要:非常輕量級的單一文件的路由器。會將真實(shí)的名作為參數(shù)傳入需要你在此方法中調(diào)度來執(zhí)行真正的請求方法。例如網(wǎng)站維護(hù)時(shí)可允許配置的值為路由將會直接執(zhí)行此路由。
inhere/sroute 非常輕量級的單一文件的路由器。簡潔、自定義性強(qiáng)
referrer the project noahbuschermacaw , but add some feature.
支持請求方法: GET POST PUT DELETE HEAD OPTIONS
支持事件: found notFound. 你可以做一些事情當(dāng)觸發(fā)事件時(shí)(比如記錄日志等)
支持設(shè)置匹配路由的解析器: SRoute::setMatchedRouteParser(). 你可以自定義如何調(diào)用匹配的路由處理程序.
支持自動匹配路由到控制器就像 yii 一樣, 請參看配置項(xiàng) autoRoute.
支持手動調(diào)度一個(gè)路由通過方法 SRoute::dispatchTo()
你也可以不配置任何東西, 它也能很好的工作
安裝{ "require": { "inhere/sroute": "dev-master" } }使用
首先, 導(dǎo)入類
use inheresrouteSRoute;添加路由
// 匹配 GET 請求. 處理器是個(gè)閉包 Closure SRoute::get("/", function() { echo "hello"; }); // 匹配參數(shù) "test/john" SRoute::get("/test/(w+)", function($arg) { echo $arg; // "john" }); // 匹配 POST 請求 SRoute::post("/user/login", function() { var_dump($_POST); }); // 匹配 GET 或者 POST SRoute::map(["get", "post"], "/user/login", function() { var_dump($_GET, $_POST); }); // 允許任何請求方法 SRoute::any("/home", function() { echo "hello, you request page is /home"; });
使用控制器方法如果配置了 "ignoreLastSep" => true, "/index" 等同于 "/index/"
SRoute::get("/index", "appcontrollersHome@index");動態(tài)匹配控制器方法
動態(tài)匹配控制器方法, 需配置 "dynamicAction" => true
NOTICE: 使用動態(tài)匹配控制器方法, 應(yīng)當(dāng)使用 any() 添加路由. 即此時(shí)無法限定請求方法 REQUEST_METHOD
// 訪問 "/home/test" 將會執(zhí)行 "appcontrollersHome::test()" SRoute::any("/home/(w+)", appcontrollersHome::class); // 可匹配 "/home", "/home/test" 等 SRoute::any("/home(/w+)?", appcontrollersHome::class);
使用方法執(zhí)行器上面兩個(gè)的區(qū)別是 第一個(gè)無法匹配 /home
配置 actionExecutor 為你需要的方法名,例如配置為 "actionExecutor" => "run",那所有的方法請求都會提交給此方法。
會將真實(shí)的action名作為參數(shù)傳入run($action), 需要你在此方法中調(diào)度來執(zhí)行真正的請求方法。
在你需要將路由器整合到自己的框架時(shí)很有用
示例:
// 訪問 "/user", 將會調(diào)用 appcontrollersUser::run("") SRoute::get("/user", "appcontrollersUser"); // 訪問 "/user/profile", 將會調(diào)用 appcontrollersUser::run("profile") SRoute::get("/user/profile", "appcontrollersUser"); // 同時(shí)配置 "actionExecutor" => "run" 和 "dynamicAction" => true, // 訪問 "/user", will call appcontrollersUser::run("") // 訪問 "/user/profile", will call appcontrollersUser::run("profile") SRoute::get("/user(/w+)?", "appcontrollersUser");自動匹配路由到控制器
支持自動匹配路由到控制器就像 yii 一樣, 需配置 autoRoute.
"autoRoute" => [ "enable" => 1, // 啟用 "controllerNamespace" => "examplescontrollers", // 控制器類所在命名空間 "controllerSuffix" => "Controller", // 控制器類后綴 ],匹配所有
配置 "matchAll" 可用于攔截所有請求。 (例如網(wǎng)站維護(hù)時(shí))
可允許配置 "matchAll" 的值為
路由path
"matchAll" => "/about", // a route path
將會直接執(zhí)行此路由。
回調(diào)
"matchAll" => function () { echo "System Maintaining ... ..."; },
將會直接執(zhí)行此回調(diào)
設(shè)置事件處理(if you need)SRoute::any("/404", function() { echo "Sorry,This page {$_GET["path"]} not found."; });
// 成功匹配路由 SRoute::on(SRoute::FOUND, function ($uri, $cb) use ($app) { $app->logger->debug("Matched uri path: $uri, setting callback is: " . is_string($cb) ? $cb : get_class($cb)); }); // 當(dāng)匹配失敗, 重定向到 "/404" SRoute::on("notFound", "/404"); // 或者, 當(dāng)匹配失敗, 輸出消息... SRoute::on("notFound", function ($uri) { echo "the page $uri not found!"; });設(shè)置配置(if you need)
// set config SRoute::config([ "stopOnMatch" => true, "ignoreLastSep" => true, "dynamicAction" => true, // "matchAll" => "/", // a route path // "matchAll" => function () { // echo "System Maintaining ... ..."; // }, // enable autoRoute, work like yii framework // you can access "/demo" "/admin/user/info", Don"t need to configure any route "autoRoute" => [ "enable" => 1, "controllerNamespace" => "examplescontrollers", "controllerSuffix" => "Controller", ], ]);
默認(rèn)配置如下
// 所有的默認(rèn)的配置 [ // stop on matched. only match one "stopOnMatch" => true, // Filter the `/favicon.ico` request. "filterFavicon" => false, // ignore last "/" char. If is True, will clear last "/", so "/home" equals to "/home/" "ignoreLastSep" => false, // match all request. // 1. If is a valid URI path, will match all request uri to the path. // 2. If is a callable, will match all request then call it "matchAll" => "", // eg: "/site/maintenance" or `function () { echo "System Maintaining ... ..."; }` // auto route match @like yii framework "autoRoute" => [ // If is True, will auto find the handler controller file. "enable" => false, // The default controllers namespace, is valid when `"enable" = true` "controllerNamespace" => "", // eg: "appcontrollers" // controller suffix, is valid when `"enable" = true` "controllerSuffix" => "", // eg: "Controller" ], // default action method name "defaultAction" => "index", // enable dynamic action. // e.g // if set True; // SRoute::any("/demo/(w+)", appcontrollersDemo::class); // you access "/demo/test" will call "appcontrollersDemo::test()" "dynamicAction" => false, // action executor. will auto call controller"s executor method to run all action. // e.g // `run($action)` // SRoute::any("/demo/(:act)", appcontrollersDemo::class); // you access `/demo/test` will call `appcontrollersDemo::run("test")` "actionExecutor" => "", // "run" ]
開始路由分發(fā)NOTICE: 必須在調(diào)用 SRoute::dispatch() 之前使用 SRoute::config() 來進(jìn)行一些配置
SRoute::dispatch();運(yùn)行示例
你可以通過 bash ./php_server 來運(yùn)行一個(gè)測試服務(wù)器, 現(xiàn)在你可以訪問 http://127.0.0.1:5670
項(xiàng)目地址github
git@osc
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/23035.html
摘要:我采用原生編寫后臺,因?yàn)楦杏X增刪改查的功能很簡單,就懶得用框架了其實(shí)是不會。瀏覽模式它也有一個(gè),用來切換文章列表和文章詳情,也就是和編輯模式它加載了作為工具欄,然后可以進(jìn)行文章的撰寫與修改。 介紹 項(xiàng)目地址:https://github.com/jrainlau/MintloG (特別亂,參考就好-_-|||)showImg(https://segmentfault.com/img/b...
摘要:最適合入門的初級教程四路由可以分發(fā)請求路由中還可以引入頁面我們可以在中搞定一切了但是如果把業(yè)務(wù)邏輯都寫入到路由中那路由將龐大的難以維護(hù)于是控制器就有了很明顯的存在價(jià)值把業(yè)務(wù)邏輯寫在控制器中路由只負(fù)責(zé)轉(zhuǎn)發(fā)請求到指定的控制器即可那我們開始創(chuàng)建控 最適合入門的Laravel初級教程(四) 路由可以分發(fā)請求; 路由中還可以引入 html 頁面;我們可以在 route/web.php 中搞定一切...
摘要:本篇文章主要是我在開發(fā)前研究了的單頁面應(yīng)用,因?yàn)樾枰玫降模源_保安裝了,建議官網(wǎng)安裝最新的穩(wěn)定版本。本文章只是和大家探討怎么利用配合做一個(gè)單頁面應(yīng)用,具體關(guān)于里面的內(nèi)容怎么寫并不在本篇文章的介紹范圍。 本篇文章主要是我在開發(fā)前研究了webpack+vue.js的單頁面應(yīng)用,因?yàn)樾枰玫絥ode的npm,所以確保安裝了node,建議官網(wǎng)安裝最新的穩(wěn)定版本。并且在項(xiàng)目中需要加載一些np...
摘要:顯示為顯示為顯示為單頁面應(yīng)用用戶訪問軌跡埋點(diǎn)開發(fā)過單頁面應(yīng)用的同學(xué),一定比較清楚,單頁面應(yīng)用的路由切換是無感知的,不會重新進(jìn)行請求去獲取頁面,而是通過改變頁面渲染視圖來實(shí)現(xiàn)。 前言 最近開發(fā)的埋點(diǎn)項(xiàng)目,需要記錄用戶行為軌跡即用戶頁面訪問順序。需要在頁面跳轉(zhuǎn)的時(shí)候,記錄用戶訪問的信息(比如 url ,請求頭部等),非單頁面應(yīng)用可以給 window 對象加上一個(gè) beforeunload ...
摘要:趁著周末偷來一點(diǎn)閑,總結(jié)近期的工作和學(xué)習(xí),想著該花點(diǎn)心思把這套基于的單頁應(yīng)用模板簡單的給介紹一下。同時(shí)也是一套可擴(kuò)展的單頁應(yīng)用開發(fā)模板。 趁著周末偷來一點(diǎn)閑,總結(jié)近期的工作和學(xué)習(xí),想著該花點(diǎn)心思把N3-admin這套基于N3-components的單頁應(yīng)用模板簡單的給介紹一下。 首發(fā)于個(gè)人博客;blog.lxstart.net項(xiàng)目路徑: https://github.com/N3-co...
閱讀 1946·2021-11-24 09:39
閱讀 3530·2021-09-28 09:36
閱讀 3302·2021-09-06 15:10
閱讀 3459·2019-08-30 15:44
閱讀 1166·2019-08-30 15:43
閱讀 1811·2019-08-30 14:20
閱讀 2724·2019-08-30 12:51
閱讀 2046·2019-08-30 11:04