How does the AltoRouter GET POST method work?

I've tried this alloruter for several weeks now. This is a good router with a small number of working examples, either on networks or on the official website. You need to somehow understand this and do your job.

I tried the basic GET and POST using altorouter and I don’t know if this is done correctly.

Simple get method in php

<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>

How I did it using 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 (receiving method)

<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"]; ?>

For some strange reason, this works, but I feel this is wrong. Reason: the information sent using the GET method is visible to everyone, the variables are displayed in the URL, you can mark the page. As the url that I get after submitting the form, this is

http://localhost/altrouter/contactus/

No variable is displayed after submitting the form in the 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>

, -, URL-

http://localhost/altrouter/aboutus/

, , .

+4
1

, , ... , :


, GET, , URL

, HTTP- GET, ?name=Joe&email=joe@example.com URL- " ". POST , URL-, ( , ), , , .


GET vs POST . , , , , . , , :

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

"MVC", , , . MVC, ​​, Lumen, , , , .


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

http://localhost/altrouter/contactus/ http://localhost/altrouter/welcome/ url welcome. .. " ".


URL, ,

http://localhost/altrouter/contactus/

, , ​​, , http://localhost/altrouter/welcome/


$_SERVER["PHP_SELF"]. . URL-. POST URL , .

+4

Source: https://habr.com/ru/post/1667099/


All Articles