4

我已经尝试这个 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/

请让我知道什么是对的,什么是错的。

4

1 回答 1

4

我想我不明白你在问什么……不过,我确实有一些观察:


使用 GET 方法发送的信息对所有人可见,变量显示在 URL 中

是的,这发生在 HTTP 方法 GET 中,?name=Joe&email=joe@example.comurl 末尾的称为“查询字符串”。它与方法 POST 的区别之一是数据是 url 的一部分,因此它是可见的(尽管不要相信它在其他情况下不可见),并且正如您所说,它可以被添加书签。


在 GET 与 POST 上,阅读这些方法的用法,并为每条路由选择一个。我不认为将多个方法映射到单个控制器是好的设计,更不用说易于维护了。利用路由器,映射不同的方法,例如:

$router->map('GET','/contactus', 'showContactForm');
$router->map('POST','/contactus', 'processContactForm');

由于您用“MVC”标记问题,因此您可以进一步分离事物,让您的控制器只是控制器,然后依次调用或生成视图。或者,您可以只使用完整的 MVC 框架,甚至是像Lumen这样的轻量级框架,它可以管理路由、视图模板、数据库连接、身份验证等等。


<form action="../welcome/" method="post">

fromhttp://localhost/altrouter/contactus/http://localhost/altrouter/welcome/相对 url 可以只是welcome. 意思是“..上一个目录”。


我提交表单后得到的 URL 是这个

http://localhost/altrouter/contactus/

我不明白为什么,如果表格如你所说成功提交,你应该在http://localhost/altrouter/welcome/


避免$_SERVER["PHP_SELF"]。它带来了不安全感。没有 action 属性的表单只会提交到相同的 url。使用 POST 方法,您可以对同一个 url 分别处理这两个操作,就像我之前所说的那样。

于 2017-01-20T14:43:20.993 回答