简体   繁体   English

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

[英]Is this how AltoRouter GET POST method work?

I have been trying out this altorouter for weeks now. 我已经尝试了几周这个altorouter。 This is looks to be good router with not many working example either on the nets or the official site. 这看起来是很好的路由器,在网络或官方网站上没有很多工作示例。 You need to understand it somehow and get the job done. 你需要以某种方式理解它并完成工作。

I tried the basic GET and POST using the altorouter and do not know whether this is the right way of doing it. 我尝试使用altorouter进行基本的GET和POST,并且不知道这是否是正确的方法。

Simple GET method in php 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>

The way I did it using AltoRouter 我使用AltoRouter的方式

Index.php 的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 Method) 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

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

For some odd reason this works but I feel this isn't right. 由于一些奇怪的原因,这有效,但我觉得这是不对的。 Reason: Information sent with the GET method is visible to everyone, the variables are displayed in the URL, it is possible to bookmark the page.Where as the URL that I get after submitting the form is this 原因:使用GET方法发送的信息对所有人都可见,变量显示在URL中,可以为页面添加书签。在提交表单后我获得的URL就是这个

http://localhost/altrouter/contactus/

No variable displayed after submitting the form in the URL. 在URL中提交表单后没有显示变量。

Now for the POST method, this one works you need to let me know is this how we are supposed to do it or not. 现在对于POST方法,这个工作你需要让我知道这是我们应该怎么做。

Index.php 的index.php

same as the one posted above

aboutus.php (POST method used) 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>

This works and the data posted is echo'ed out, URL after submitting 这有效,发布的数据在提交后回显,URL

http://localhost/altrouter/aboutus/

Please let me know what is right and what is wrong. 请让我知道什么是对的,什么是错的。

I don't think I understand what you are asking... I do have some observations, though: 我不认为我明白你在问什么......我确实有一些观察:


Information sent with the GET method is visible to everyone, the variables are displayed in the URL 使用GET方法发送的信息对每个人都可见,变量显示在URL中

Yes, that happens in HTTP method GET, the ?name=Joe&email=joe@example.com at the end of the url is called "query string". 是的,这发生在HTTP方法GET中,url末尾的? ?name=Joe&email=joe@example.com称为“查询字符串”。 One of its differences with method POST is that the data is part of the url, so it's visible (alhtough don't trust that it is not visible otherwise) and as you say it can be bookmarked. 与POST方法的不同之处在于数据是url的一部分,因此它是可见的(尽管不相信它不可见 ),正如你所说它可以被加入书签。


On GET vs POST, read about the usage of those methods and decide one for each route. 在GET和POST上,阅读这些方法的用法并为每个路由确定一个。 I don't think it's good design, let alone easily maintainable, to have several methods mapped to a single controller. 我不认为将几个方法映射到单个控制器是好的设计,更不用说容易维护了。 Take advantage of the router, map different methods, for instance: 利用路由器,映射不同的方法,例如:

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

Since you tag the question with "MVC", you could separate things further and have your controllers be just controllers which in turn call or generate views. 由于您使用“MVC”标记问题,您可以进一步分离事物并让控制器只是控制器,而控制器又调用或生成视图。 Or, you can just use a full MVC framework, even a light one like Lumen , which manages routing, view templates, database connection, authentication and much more. 或者,您可以使用完整的MVC框架,甚至是像Lumen这样的轻量级框架,它管理路由,视图模板,数据库连接,身份验证等等。


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

From http://localhost/altrouter/contactus/ to http://localhost/altrouter/welcome/ the relative url can be just welcome . http://localhost/altrouter/contactus/http://localhost/altrouter/welcome/可以welcome相关网址。 The .. means "go up a directory". ..表示“上一个目录”。


the URL that I get after submitting the form is this 提交表单后我得到的URL是这个

http://localhost/altrouter/contactus/

I don't get why, if the form submitted successfully as you say, you should be in http://localhost/altrouter/welcome/ 我不明白为什么,如果表格提交成功,如你所说,你应该在http://localhost/altrouter/welcome/


Avoid $_SERVER["PHP_SELF"] . 避免使用$_SERVER["PHP_SELF"] It brings insecurities . 它带来了不安全感 A form with no action attribute will just submit to the same url. 没有操作属性的表单只会提交到同一个网址。 With method POST, you can, for the same url, handle both actions separately as I said earlier. 使用POST方法,对于同一个url,您可以像我之前所说的那样单独处理这两个操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM