Jump to content

How to send specific HTTP status code and go to error page


DrQuincy
 Share

Recommended Posts

I know you can throw a 404 easily enough:

throw new \ProcessWire\Wire404Exception(); 

However, is there an easy way to throw other HTTP status codes that sends the appropriate HTTP header and loads the error page rather than the current template?

For example, I have some pages where I want to restrict the request method to GET or POST only and therefore want to sent a 405 Method Not Allowed if the wrong method is requested. I don't just want to manually send the headers and exit() though — I want the error page to show and for ProcessWire to finish the request.

Is this easy to do? I looked but couldn't find anything.

Thanks.

  • Like 1
Link to comment
Share on other sites

Thanks, I wasn't aware of the $http variable. ?

Looking through the functions I think possibly this is more suitable in this instance. https://processwire.com/api/ref/wire-http/send-status-header/ I'm sure HEAD work work — though I may be wrong!

What I am wanting to do is send something other than a 200 status for the current request. It looks like $http->sendStatusHeader(405) would send the right header per my above exmaple. Is it possible to then show the error page template?

Link to comment
Share on other sites

Not sure if this is the best way as it involves adding code to every template file but this seems to work.

Add a function in functions.php such as killWithstatusCode() that returns a simple HTML error template. This can check the current HTTP response code and output a message accordingly. Then at the top of each template add:

if (http_response_code() != 200) return killWithstatusCode();

Technically, the response code could be between 200 and 299 and be regarded as a success so you could replace http_response_code() with a more concise function that checks if the code starts with a 2.

if (!statusCodeSuccess()) return killWithstatusCode();

I did see Ryan say in an old forum post that if you don't wish to render a template then you can simply call return and ProcessWire will still handle the process. I also noticed that whatever string you return from the template ProcessWire will render.

You might wonder the use case for this. I find it useful to be able to allow a CMS to use HTTP response codes that typically the server would handle. Being able to send 405 Method Not Allowed, for example, when creating a RESTful API or handling POST forms can be useful. Or if you wish to block POST requests to a certain page. Also, 400, 401, 403 and 500 are useful codes when building web apps.

I have adapted ProcessWire to use a MVP pattern so this means I can send out a non-2XX status code before the view (template) is rendered and not have to worry about that part.

Unlike a 404, these errors are generally unlikely to be encountered under normal usage are are more indicative of server or application error and so it doesn't matter that the full blown ProcessWire error page is not shown.

If there's a better way I'm all ears. ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...