phpmodel-view-controllerroutesrouteraltorouter

Is this how AltoRouter GET POST method work?


I have been trying out this altorouter for weeks now. 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.

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>

The way 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 (Get 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 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

http://localhost/altrouter/contactus/

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

Index.php

same as the one posted above

aboutus.php (POST method used)

<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

http://localhost/altrouter/aboutus/

Please let me know what is right and what is wrong.


Solution

  • 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

    Yes, that happens in HTTP method GET, the ?name=Joe&email=joe@example.com at the end of the url is called "query string". 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.


    On GET vs POST, read about the usage of those methods and decide one for each route. 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. 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.


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

    From http://localhost/altrouter/contactus/ to http://localhost/altrouter/welcome/ the relative url can be just welcome. The .. means "go up a directory".


    the URL that I get after submitting the form is this

    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/


    Avoid $_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.