From 0f3aa8c50f0b4bd9a9b94f2a03cd029cd3ef164f Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 2 Nov 2023 17:25:52 +0800 Subject: [PATCH] Update Router --- Router.md | 59 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/Router.md b/Router.md index 67e306c..9519f79 100644 --- a/Router.md +++ b/Router.md @@ -1,4 +1,8 @@ -框架的路由, 其实是内置的一个中间件, 非常简洁高效。 +# 路由 +> 框架内置一个简洁高效的 **路由中间件**,框架的理念是, 让开发者不再需要过多关注路由, 所以请求的路径直接对应着控制器文件名。开发者无需编写任何路由规则。 + +## 路由模式一 +> 这是默认的模式。 先假设几个访问地址, 如下: @@ -8,12 +12,12 @@ http://abc.com/foo/bar http://abc.com/aaa/bbb/ccc/ddd ``` -+ 先看第1个, 光杆域名, 没有任何路径。 - > 这种情况, 路由会默认为 `/index`, 即, 此时路由会在apps目录中寻找一个叫index.js的文件, 如果能找到, 就实例化它, 如果找不到就抛出404,并提示`Controller [index] not found`。 ++ 先看第1个, 没有任何路径。 +> 这种情况, 路由会默认为 `/index`, 即, 此时路由会在apps目录中寻找一个叫index.js的文件, 如果能找到, 就实例化它, 如果找不到就抛出404,并提示`Controller [index] not found`。 - > 这里继续讲解 index.js存在的情况, 实例化之后呢? 这时候, 就要看url路径中的第2段值, 这里因为是光杆路径, 所以会默认为 `/index/index`, 即 `http://abc.com` 等价于 `http://abc.com/index/index`。 +> 这里继续讲解 index.js存在的情况, 实例化之后呢? 这时候, 就要看url路径中的第2段值, 这里因为没有路径, 所以会默认为 `/index/index`, 即 `http://abc.com` 等价于 `http://abc.com/index/index`。 - > 于是, index.js被实例化之后, 又会被自动调用`indexAction()` 方法, 这里的方法名为什么会多出一个`Action`呢? 这是为了避免路径的值可能会命中js保留关键字, 所以特意设计的, 这也是`YAF框架`的做法, 很简单, 却很实用。 +> 于是, index.js被实例化之后, 又会被自动调用`indexAction()` 方法, 这里的方法名为什么会多出一个`Action`呢? 这是为了避免路径的值可能会命中js保留关键字, 所以特意设计的, 这也是`YAF框架`的做法, 很简单, 却很实用。 + 接下来看第2个url, 这里不再是光杆域名了。 @@ -28,12 +32,49 @@ http://abc.com/aaa/bbb/ccc/ddd > 拿这个例子来说, 最终就是 寻找到aaa.js文件, 实例化调用,`bbbAction('ccc', 'ddd')` -这样一看, 是否已经完全明白路由的工作流程了呢。 +## 路由模式二 +> 通常情况下, 使用**模式一**, 完全能满足99.9999%的需求。但是偶尔业务需要动态路由, 但是又不想用户请求的路径过长。那便可开启**模式二**。 + + +```js +import { createApp } from '@gm5/core' + +const app = createApp({ dynamic: true }) + +// or +app.set({ dynamic: true }) + +``` + +开启动态路由模式, 无论是访问路径是什么, 永远只会调用 `\apps\index\indexAction()`, 剩余的路径, 会以参数形式, `传入indexAction()`中 + + + + + + + + + + + + + + + + + + + + + + + + + + ----- -上一节: [⟪默认配置⟫](https://github.com/bytedo/gmf.core/wiki/Config) -下一节: [⟪跨域配置⟫](https://github.com/bytedo/gmf.core/wiki/Cors)