我一直在嘗試這個altorouter幾個星期了。這看起來是一個很好的路由器,在網絡或官方網站上沒有太多的工作實例。你需要以某種方式理解它並完成工作。AltoRouter GET POST方法是如何工作的?
我使用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
的index.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的方法(GET方法)
<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>
的welcome.php
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
對於一些奇怪的原因,這個工作,但我覺得這是不對的。原因:用GET方法發送信息的人都能看到,變量顯示在URL,它可能爲書籤的page.Where爲我提交表單後,即可獲取URL是這個
http://localhost/altrouter/contactus/
無在URL中提交表單後顯示的變量。
現在對於POST方法,這個工作你需要讓我知道這是我們應該如何做或不做。
的index.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>
這工作和數據被貼出來echo'ed,提交
後URLhttp://localhost/altrouter/aboutus/
請讓我知道什麼是對的,什麼是錯的。