简体   繁体   中英

PHP call class via html form

just thinking about something. I have a standard html form. On the backend I use PHP to handle some APIs, and I have made everything Object Orientated. I am happy with everything besides one thing. The PHP file the forms action calls looks like so

<?php

include 'APIConnection.php';

if (isset($_POST["emailAddress"]) && !empty($_POST["emailAddress"])){
    $email = $_POST["emailAddress"];
    $connection = new APIConnection();
    $response = $connection->getData($email);
}

So in the midst of some nice OO structure is a random file which invokes my classes. Just seems a bit out of place to me. Is this common practice? I was thinking how I could keep things OO, and I was thinking about making another class which the form can call, and have the above basically performed within the classes constructor.

I am just hoping for thoughts really as to what the correct way to handle this is. I know the above works, but to me it feels like it messes up all the OO code I have done so far because its not OO.

Thanks

Eg you could do something like:

<?php

// new InputHandler class in file: InputHandler.php
class InputHandler {

    public static function post($param_name, $default_value) {
        return (array_key_exists($param_name, $_POST) && !is_null($_POST[$param_name]) ? $_POST[$param_name] : $default_value);
    }

    public static function sanitize_email($value) {
        return filter_var($value, FILTER_SANITIZE_EMAIL);
    }

    public static function validate_email($value) {
        return filter_var($value, FILTER_VALIDATE_EMAIL);
    }
}

And in your other php file:

<?php
require_once 'APIConnection.php';
// load new class for handling inputs
require_once 'InputHandler.php';

$email = InputHandler::post("emailAddress", '');
$email = InputHandler::sanitize_email($email);

if (InputHandler::validate_email($email)) {
    $connection = new APIConnection();
    $response = $connection->getData($email);
}

It is not a complete solution, but may be a start, depending, what you mean by OO code and your needs !)

But, personally, I prefer using composer for managing dependencies and autoloading as a project grows. And there are plenty of written libraries for you to handle input data and load via composer. Google it!

It doesn't sound like you're using a framework but the file that the form posts to is essentially your router. It's just specific to this form in this case. If you wanted something more generic you could route all of your requests to one file and handle all your loading there. You could accomplish this with a mod_rewrite. Example below uses index.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*+)$ index.php?q=$1 [L]

I don't know your code structure but index.php could load the appropriate controller based on the route or appropriate API if that's all your codebase is. This would be a RESTful style approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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