这是AltoRouter GET POST方法的工作原理吗?

时间:2017-01-18 07:16:12

标签: php model-view-controller routes router altorouter

我已经尝试了几周这个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

的方式

的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(获取方法)

<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中,可以为页面添加书签。在提交表单后我得到的URL就是这个

http://localhost/altrouter/contactus/

在网址中提交表单后没有显示变量。

现在对于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>

这是有效的,发布的数据在提交后回显出来,网址

http://localhost/altrouter/aboutus/

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

1 个答案:

答案 0 :(得分:4)

我不认为我理解你在问什么...我确实有一些观察:

  

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

是的,这发生在HTTP方法GET中,url末尾的?name=Joe&email=joe@example.com被称为&#34;查询字符串&#34;。与POST方法的不同之处在于数据是网址的一部分,因此它是可见的(尽管不相信它不是可见否则)正如你所说的那样它可以加入书签。

在GET vs POST上,阅读有关这些方法的用法并为每条路线确定一个方法。我不认为它有好的设计,更不用说容易维护了,有几种方法映射到一个控制器。利用路由器,映射不同的方法,例如:

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

由于您使用&#34; MVC&#34;标记问题,您可以进一步分离事物,让控制器只是控制器,而控制器又调用或生成视图。或者,您可以使用完整的MVC框架,甚至是Lumen之类的轻量级框架,它可以管理路由,视图模板,数据库连接,身份验证等等。

  

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

http://localhost/altrouter/contactus/http://localhost/altrouter/welcome/,相对网址可以只是welcome..表示&#34;上一个目录&#34;。

  

提交表单后我得到的网址是

     

http://localhost/altrouter/contactus/

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

避免使用$_SERVER["PHP_SELF"]。它带来insecurities。没有操作属性的表单只会提交到同一个网址。使用方法POST,您可以为同一个url单独处理这两个操作,如前所述。