simplfuzz
simplfuzz

Reputation: 12905

How to Automatically return 405 when missing parameters in controllers

In the Spring Framework for Java, Controllers return 405 Method Not Allowed if one or more required parameters are missing in the request. Is there a smart or easy way of doing the same in PHP?

Upvotes: 0

Views: 90

Answers (2)

gries
gries

Reputation: 1143

most of the big frameworks provide such functionality if your going selfmade a function like this that is triggerd when validating the controller parameters.

function forward_not_allowed()
{
    header("Status: 405 Method Not Allowed");
    exit;
}

if you want something thats a little more advanced and automated you could register your own error-handler that parses the php-waring/errors that are thrown if a method is called with invalid parameters and create the redirect there.http://phpmaster.com/error-handling-in-php/

Upvotes: 0

Nanne
Nanne

Reputation: 64409

Yes or no, depending on what you call smart and/or easy. But (see below) I guess you would say "no".

405 Method Not Allowed is a HTTP status code and PHP will not return these at all by default (apart from sending the default 200 header if you go trough apache for instance).

There is no 'controller' in standard PHP, so there is nothing for you to expect a return from. You could use or build a framework, and that might return something if there is a parameter missing.

Return a header like that would look like this, if you're going to implement it yourself.

header('HTTP/1.1 405 Method Not Allowed');

Upvotes: 2

Related Questions