我已经尝试这个 altorouter 好几个星期了。这看起来是一个很好的路由器,在网上或官方网站上都没有多少工作示例。你需要以某种方式理解它并完成工作。
我使用 altorouter 尝试了基本的 GET 和 POST,但不知道这是否是正确的做法。
php中的简单GET方法
<html>
<head>
</head>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
我使用 AltoRouter 的方式
索引.php
<?php
require 'library/AltoRouter.php';
$router = new AltoRouter();
$router->setBasePath('/AltRouter');
$router->map('GET','/', function() {require __DIR__ . '/catalog/controller/home.php';}, 'home');
$router->map('GET|POST','/aboutus/', function() {require __DIR__ . '/catalog/controller/aboutus.php';}, 'aboutus');
$router->map('GET|POST','/contactus/', function() {require __DIR__ . '/catalog/controller/contactus.php';}, 'contactus');
$router->map('GET|POST','/welcome/', function() {require __DIR__ . '/catalog/controller/welcome.php';}, 'welcome');
$match = $router->match();
if( $match && is_callable( $match['target'] ) ) {
call_user_func_array( $match['target'], $match['params'] );
} else {
// no route matched
header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}
contactus.php(获取方法)
<html>
<head>
</head>
<body>
<form action="../welcome/" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
欢迎.php
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
出于某种奇怪的原因,这可行,但我觉得这是不对的。原因:用GET方法发送的信息对所有人可见,变量显示在URL中,可以将页面添加到书签中。我提交表单后得到的URL是这个
http://localhost/altrouter/contactus/
在 URL 中提交表单后不显示任何变量。
现在对于 POST 方法,这个方法有效,你需要让我知道我们应该如何做。
索引.php
same as the one posted above
aboutus.php(使用 POST 方法)
<html>
<head>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["first_name"];
$email = $_POST["email_address"];
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
}
?>
<form action="<?php $_SERVER["PHP_SELF"]?>" method="post">
Name: <input type="text" name="first_name">
<br><br>
E-mail: <input type="text" name="email_address">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
这有效,并且发布的数据被回显,提交后的 URL
http://localhost/altrouter/aboutus/
请让我知道什么是对的,什么是错的。