I have a php class, RegistrationController,
<?php
class RegistrationController extends ParentController {
function __construct() {
parent::__construct();
echo 'We are inside RegistrationController';
}
public function register() {
$name = $_GET['name'];
echo "<li> We will register soon, $name </li>";
}
}
?>
I am accessing the register method via URL like this, [FYI, I am using RewriteEngine in .htaccess]
http://localhost:8888/Service/RegistrationController/register?name=Owl
Question
Now, everything works fine, I just need to make sure my queries have those ?name=Owl variables set when I request some computation.
The web service will not be accessible from Web Browser it’s just a trivial service I want to made for my Mobile application. The application will be calling this URL as it is without any change except the details (var).
So, is it okay to $_GET or $_POST inside class’s function? Is it scalable or maintainable?
Additional Info
I am calling the methods of different controllers in very generic manner, so In my Bottstrap I am just calling this function which lets me call any controller’s method with few lines.
private function handleMethodCallOn($controller, $url) {
//check if the URL has specified any method to call
if(isset($url[1])) {//URL has been exploded earlier to get content
if(method_exists($controller, $url[1])) {
$controller->{$url[1]}();//HERE I am calling any function
} else {
$error = new error_404();
}
}
}
It is perfectly viable to use post and get inside a class method. What jumps out at experienced devs about your code is more likely to be 2 things:
1. lack of input validation
2. dumping user input directly into your page’s output (one can only hope that you aren’t doing similar things on the way into the database).
If you are not interested in using a framework, you can do these things manually.
However, there are myriad solid PHP frameworks which can bootstrap a RESTful request for you, abstracting it into an object, rich with input validation and the like.