Skip to content

Commit aaaee8a

Browse files
committed
fix: url bug
1 parent a6ef927 commit aaaee8a

4 files changed

Lines changed: 60 additions & 19 deletions

File tree

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"cSpell.words": [
3+
"urlarg"
4+
]
5+
}

example/get.test.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,40 @@
33

44
$app = new phpExpress();
55

6-
// Request : http://localhost:3000/test/get.test.php/3/get
6+
// Request : http://localhost:3000/example/get.test.php/3/urlarg
77
// Result : 3
8-
$app->get('/{id}/get', function ($req, $res) {
8+
$app->get('/{id}/urlarg', function ($req, $res) {
99
try {
1010
$res->send($req["id"]);
1111
} catch (Exception $e) {
1212
http_response_code(500);
13-
return $e;
13+
$res->send($e);
1414
}
1515
});
1616

17-
// Request : http://localhost:3000/test/get.test.php/test
17+
// Request : http://localhost:3000/example/get.test.php/text
1818
// Result : phpExpress
19-
$app->get('/test', function ($req, $res) {
19+
$app->get('/text', function ($req, $res) {
2020
try {
2121
$res->send("phpExpress");
2222
} catch (Exception $e) {
2323
http_response_code(500);
24-
return $e;
24+
$res->send($e);
2525
}
2626
});
2727

28-
// Request : http://localhost:3000/test/get.test.php/test2
28+
// Request : http://localhost:3000/example/get.test.php/json
2929
// Result : {"msg":"phpExpress"}
30-
$app->get('/test2', function ($req, $res) {
30+
$app->get('/json', function ($req, $res) {
3131
try {
3232
$res->send((object) ["msg" => "phpExpress"]);
3333
} catch (Exception $e) {
3434
http_response_code(500);
35-
return $e;
35+
$res->send($e);
3636
}
3737
});
3838

39-
// Request : http://localhost:3000/test/get.test.php/argument?a=1
39+
// Request : http://localhost:3000/example/get.test.php/argument?a=1
4040
// body : json {"b":2}
4141
// Result : 3
4242
$app->get('/argument', function ($req, $res) {
@@ -45,7 +45,7 @@
4545
$res->send($result);
4646
} catch (Exception $e) {
4747
http_response_code(500);
48-
return $e;
48+
$res->send($e);
4949
}
5050
});
5151

example/retrun.test.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
require '../lib/phpExpress.php';
3+
4+
$app = new phpExpress();
5+
6+
/*
7+
* If you read that data from a web browser,
8+
*/
9+
10+
// Request : http://localhost:3000/example/retrun.test.php/get
11+
// Result :
12+
$app->get('/get', function ($req, $res) {
13+
try {
14+
$res->send((object) ["msg" => "phpExpress"]);
15+
} catch (Exception $e) {
16+
http_response_code(500);
17+
$res->send($e);
18+
}
19+
});
20+
21+
// Request : http://localhost:3000/example/retrun.test.php/post
22+
// Result : {"msg": "phpExpress"}
23+
$app->post('/post', function ($req, $res) {
24+
try {
25+
$res->send((object) ["msg" => "phpExpress"]);
26+
} catch (Exception $e) {
27+
http_response_code(500);
28+
$res->send($e);
29+
}
30+
});
31+
32+
// 404 return
33+
$app->listen();

lib/phpExpress.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,14 @@ public function listen()
7575
*/
7676
private function removePhpUrl($url)
7777
{
78-
$result = substr($url, strpos($url, ".php") + 4);
79-
return substr($result, 0, strpos($result, "?"));
78+
$result = $url; // org url
79+
if (strpos($url, ".php")) {
80+
$result = substr($url, strpos($url, ".php") + 4);
81+
}
82+
if (strpos($result, "?")) {
83+
$result = substr($result, 0, strpos($result, "?"));
84+
}
85+
return $result;
8086
}
8187

8288
/*
@@ -85,13 +91,8 @@ private function removePhpUrl($url)
8591
*/
8692
private function convertParm($parm)
8793
{
88-
// If Same parm
89-
if (strcmp($_SERVER['REQUEST_URI'], $parm)) {
90-
return $parm;
91-
}
92-
9394
$replaceParm = $parm;
94-
$count = preg_match_all('/{/u', $parm);
95+
$count = preg_match_all('/{/u', $replaceParm);
9596
for ($dataEnd = 0, $end = 0, $i = 0; $i < $count; ++$i) {
9697
// parm spread
9798
$start = strpos($parm, "{", $end) + 1;
@@ -113,6 +114,8 @@ private function convertParm($parm)
113114
public function get($parm, $function)
114115
{
115116
$parm = $this->convertParm($parm);
117+
// echo $this->removePhpUrl($_SERVER['REQUEST_URI']) . "===" . $parm . "<br/>";
118+
116119
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
117120
return;
118121
} else if (strcmp($this->removePhpUrl($_SERVER['REQUEST_URI']), $parm)) {

0 commit comments

Comments
 (0)