Jump to content

Stop current request and shutdown

Recommended Posts

I've searched for this but couldn't find anything but am maybe using the wrong terms.

Is there a way to do something like exit('<p>an error occurred!</p>'); but still have ProcessWire “shutdown” normally? Specifically, I want to be able to not render the template but still run some logging code in finished.php.

Is there a function for this?


Link to comment
Share on other sites

Posted (edited)

@DrQuincy Is this from a template file? You could do this:

echo "<p>An error occurred!</p>";
return $this->halt();

That halts the rendering of output but lets PW finish everything else in the request. 

Another option is to throw a WireException. This will also halt the request but allow PW to still shutdown. The response is an http 500 error. The error message will be displayed if in debug mode or if the user is logged in and a superuser.

throw new WireException("An error occurred!"); 

Often times on the front end a fatal error is about a resource not available, and a 404 is the appropriate response. So you can do this:

throw new Wire404Exception();

or this does the same thing (I prefer this shorter version):


Lastly, while the above are preferable, it's also okay to do an exit() or a die(), is shouldn't break anything at least. 

Edited by ryan
added return to $this->halt
  • Like 4
Link to comment
Share on other sites

Thanks to you both, some very good suggestions.

From the template file @Robin S is right, you need to use return. Actually though, just this will work:

return '<p>An error occurred</p>';

What would be the advantage of:

<p>An error occurred</p>
<?php return $this->halt(); ?>

What does halt() actually do?

I am reluctant to use exit() as I have some code in finished.php to run so the above works well for me, thanks.

Link to comment
Share on other sites

3 hours ago, DrQuincy said:

What does halt() actually do?

Here is the blog post about it: https://processwire.com/blog/posts/processwire-2.6.8-brings-new-version-of-reno-admin-theme-and-more/#new-this-gt-halt-method-for-use-in-template-files


Now you can call return $this->halt(); from within any template file, and it will prevent further output from that template file (and anything that comes after it, like an appendTemplateFile). This enables output to stop, but PW's normal execution and shutdown process to proceed normally. Please note that return $this->halt(); must be called from directly within the template file, outside of any function or class scope.


  • Like 2
Link to comment
Share on other sites

@DrQuincy Yes, sorry, it should be "return $this->halt();" rather than just $this->halt(), I have updated my example. If you don't do a return then it'll still do the same thing (see below), but instead it'll continue executing your code in that file after the halt() call. 

What $this->halt(); does is tell the current request not to render any more files. So if you call $this->halt(); from your template file, then it's not going to include any further files specified to render afterwards. For instance, it will skip rendering your _main.php file (if you are using one). But everything else about the request will continue normally, so it does a normal shutdown, etc. 

  • Like 2
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

  • Recently Browsing   0 members

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