1212## 路由收集
1313
1414``` php
15- use Inhere\Route\SRouter;
15+ use Inhere\Route\ORouter;
16+
17+ $router = new ORouter();
1618
1719// 匹配 GET 请求. 处理器是个闭包 Closure
18- SRouter:: get('/', function() {
20+ $router-> get('/', function() {
1921 echo 'hello';
2022});
2123
2224// 匹配参数 'test/john'
23- SRouter:: get('/test/{name}', function($arg) {
25+ $router-> get('/test/{name}', function($arg) {
2426 echo $arg; // 'john'
2527}, [
2628 'params' => [
@@ -29,7 +31,7 @@ SRouter::get('/test/{name}', function($arg) {
2931]);
3032
3133// 可选参数支持。匹配 'hello' 'hello/john'
32- SRouter:: get('/hello[/{name}]', function($name = 'No') {
34+ $router-> get('/hello[/{name}]', function($name = 'No') {
3335 echo $name; // 'john'
3436}, [
3537 'params' => [
@@ -38,53 +40,26 @@ SRouter::get('/hello[/{name}]', function($name = 'No') {
3840]);
3941
4042// 匹配 POST 请求
41- SRouter:: post('/user/login', function() {
43+ $router-> post('/user/login', function() {
4244 var_dump($_POST);
4345});
4446
4547// 匹配 GET 或者 POST
46- SRouter:: map(['get', 'post'], '/user/login', function() {
48+ $router-> map(['get', 'post'], '/user/login', function() {
4749 var_dump($_GET, $_POST);
4850});
4951
5052// 允许任何请求方法
51- SRouter:: any('/home', function() {
53+ $router-> any('/home', function() {
5254 echo 'hello, you request page is /home';
5355});
5456
5557// 路由组
56- SRouter::group('/user', function () {
57- SRouter::get('/', function () {
58- echo 'hello. you access: /user/';
59- });
60- SRouter::get('/index', function () {
61- echo 'hello. you access: /user/index';
62- });
63- });
64- ```
65-
66- 使用 ` ORouter ` 则需先创建对象:
67-
68- ``` php
69- use Inhere\Route\ORouter;
70-
71- $router = new ORouter;
72-
73- // 添加路由
74- // $router->get();
75- // $router->post();
76- // $router->put();
77- // ... ...
7858$router->group('/user', function ($router) {
7959 /** @var \Inhere\Route\ORouter $router */
80- $router->get('', function () {
81- echo 'hello. you access: /user';
60+ $router->get('/ ', function () {
61+ echo 'hello. you access: /user/ ';
8262 });
83-
84- //$router->get('/', function () {
85- // echo 'hello. you access: /user/';
86- //});
87-
8863 $router->get('/index', function () {
8964 echo 'hello. you access: /user/index';
9065 });
@@ -99,23 +74,80 @@ $router->group('/user', function ($router) {
9974
10075#### 1. 静态路由
10176
77+ 整个路由 path 都是静态字符串 e.g. '/user/login'
78+
10279例如:
10380
10481``` php
10582$router->post('/user/signUp', 'handler2');
10683```
10784
85+ - 存储结构
86+
87+ ``` php
88+ array (
89+ '/' => array (
90+ 'GET' => array (
91+ 'handler' => 'handler0',
92+ 'option' => array (
93+ ),
94+ ),
95+ ),
96+ '/home' => array (
97+ 'GET' => array (
98+ 'handler' => 'Inhere\\Route\\Examples\\Controllers\\HomeController@index',
99+ 'option' => array (
100+ ),
101+ ),
102+ ),
103+ '/post' => array (
104+ 'POST' => array (
105+ 'handler' => 'post_handler',
106+ 'option' => array (
107+ ),
108+ ),
109+ ),
110+ '/put' => array (
111+ 'PUT' => array (
112+ 'handler' => 'main_handler',
113+ 'option' => array (
114+ ),
115+ ),
116+ ),
117+ '/del' => array (
118+ 'DELETE' => array (
119+ 'handler' => 'main_handler',
120+ 'option' => array (
121+ ),
122+ ),
123+ ),
124+ '/pd' => array (
125+ 'POST' => array (
126+ 'handler' => 'multi_method_handler',
127+ 'option' => array (
128+ ),
129+ ),
130+ 'DELETE' => array (
131+ 'handler' => 'multi_method_handler',
132+ 'option' => array (
133+ ),
134+ ),
135+ ),
136+ );
137+ ```
138+
108139#### 2. (有规律的)动态路由
109140
141+ 第一节是个静态字符串,称之为有规律的动态路由。按第一节的信息进行分组存储
142+
110143例如:
111144
112145``` php
113146/*
114147match:
115148 /hello/tom
116- /hello
117149 */
118- $router->get('/hello[ /{name}] ', function($name='NO') {
150+ $router->get('/hello/{name}', function($name='NO') {
119151 echo "hello, $name"; // 'john'
120152},[
121153 'params' => [
@@ -124,8 +156,47 @@ $router->get('/hello[/{name}]', function($name='NO') {
124156]);
125157```
126158
159+ - 存储结构
160+
161+ ``` php
162+ 'user' => array (
163+ 0 => array (
164+ 'regex' => '#^/user/(?P<id >[1-9][0-9]*)$#',
165+ 'start' => '/user/',
166+ 'original' => '/user/{id}',
167+ 'handler' => 'main_handler',
168+ 'option' => array (
169+ ),
170+ 'methods' => 'GET',
171+ ),
172+ 1 => array (
173+ 'regex' => '#^/user/(?P<id >[1-9][0-9]*)$#',
174+ 'start' => '/user/',
175+ 'original' => '/user/{id}',
176+ 'handler' => 'main_handler',
177+ 'option' => array (
178+ ),
179+ 'methods' => 'POST',
180+ ),
181+ ),
182+ 'home' => array (
183+ 0 => array (
184+ 'regex' => '#^/home/(?P<act >[a-zA-Z][\\w-]+)$#',
185+ 'start' => '/home/',
186+ 'original' => '/home/{act}',
187+ 'handler' => 'Inhere\\Route\\Examples\\Controllers\\HomeController',
188+ 'option' => array (
189+ ),
190+ 'methods' => 'ANY,GET,POST,PUT,PATCH,DELETE,OPTIONS,HEAD,SEARCH,CONNECT,TRACE',
191+ ),
192+ ),
193+ )
194+ ```
195+
127196#### 3. (无规律的)动态路由
128197
198+ 第一节就包含了正则匹配,称之为无规律/模糊的动态路由
199+
129200例如:
130201
131202``` php
@@ -136,6 +207,52 @@ $router->get('/{name}', 'default_handler', [
136207]);
137208```
138209
210+ - 存储结构
211+
212+ ``` php
213+ array (
214+ 'GET' => array (
215+ 0 => array (
216+ 'regex' => '#^/about(?:\\.html)?$#',
217+ 'include' => '/about',
218+ 'original' => '/about[.html]',
219+ 'handler' => 'Inhere\\Route\\Examples\\Controllers\\HomeController@about',
220+ 'option' => array (
221+ ),
222+ ),
223+ 1 => array (
224+ 'regex' => '#^/(?P<name >blog|saying)$#',
225+ 'include' => NULL,
226+ 'original' => '/{name}',
227+ 'handler' => 'default_handler',
228+ 'option' => array (
229+ 'params' => array (
230+ 'name' => 'blog|saying',
231+ ),
232+ ),
233+ ),
234+ 2 => array (
235+ 'regex' => '#^/test(?:/optional)?$#',
236+ 'include' => '/test',
237+ 'original' => '/test[/optional]',
238+ 'handler' => 'default_handler',
239+ 'option' => array (
240+ ),
241+ ),
242+ 3 => array (
243+ 'regex' => '#^/blog-(?P<post >[^/]+)$#',
244+ 'include' => '/blog-',
245+ 'original' => '/blog-{post}',
246+ 'handler' => 'default_handler',
247+ 'option' => array (
248+ ),
249+ ),
250+ ),
251+ 'POST' => array( ... ),
252+ 'PUT' => array( ... )
253+ )
254+ ```
255+
139256## 路由匹配
140257
141258``` php
@@ -154,7 +271,7 @@ array|false public function match($path, $method)
154271$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
155272$method = $_SERVER['REQUEST_METHOD'];
156273
157- $route = SRouter:: match($path, $method);
274+ $route = $router-> match($path, $method);
158275```
159276
160277匹配失败,返回 ` false `
@@ -216,7 +333,7 @@ todo ...
216333
217334``` php
218335// set config
219- SRouter:: setConfig([
336+ $router-> setConfig([
220337 'ignoreLastSep' => true,
221338 'autoRoute' => 1,
222339 'controllerNamespace' => 'app\\controllers',
@@ -246,7 +363,7 @@ SRouter::setConfig([
246363]
247364```
248365
249- > NOTICE: 必须在添加路由之前调用 ` SRouter:: setConfig()`
366+ > NOTICE: 必须在添加路由之前调用 ` $router-> setConfig()`
250367
251368### 自动匹配路由
252369
0 commit comments