Returning specific status codes (e.g., 410 GONE)

I’m sorry for the string of plugin questions, but I’m struggling to find the information I need. Once I finish this batch of plugins, I’ll make some suggestions for the documentation.

Anyway, I can’t seem to find a plugin (including login) that returns arbitrary status codes. I’m accepting POSTs at a specific URL and want to return a variety of codes depending on the inputs: 200, 201, 202, 400, and 500, specifically (and eventually 410). The login plugin has helped me learn how to add custom routes, and I know my code is being executed, but I don’t know how to end the function—how to actually send the correct response. The login plugin only sends session messages. And as far as I can see in the code, it redirects to various places but never actually sends appropriate status codes.

Any assistance greatly appreciated.

There’s a few places where you throw a \RuntimeException('message' 404), but that doesn’t actually return a 404. The actual status code returned is still 500.

At least it does when I throw it from my code. On my actual blog, if I go to a bad URL I do indeed get a 404. I’m so confused :confused:

So I’m just going to follow the error plugin and create separate page files, using the http_response_code header. I’m just used to dumping some text, setting the code, and getting out. But if this is the best way, then so be it.

I’m going slightly crazy. Following the Error plugin model, I was able to get the Graveyard plugin working. Now I’m back to Webmention. All I’m trying to do right now is get a GET to the /mentions route to return a custom page. I’ve got it to where it’s running the twig, but it’s not generating and returning the page content itself. If someone is willing to take a look at some code, I’d be very grateful.

The code in question is here.

  • On line 71 I add my handler code (handleReceipt) on the onPagesInitialized hook. It’s getting there.

  • On line 100 the function starts. Everything there at the top is for the POST scenario, which I don’t care about right now. So we jump to…

  • Line 224 is where the GET scenario executes. I know it’s getting to this point. You’ll see I have the following code:

$this->grav['page']->init(new \SplFileInfo(__DIR__ . '/pages/'));

The markdown file [is here]( s/ And the twig file is here.

Before I had the twig file, it complained it wasn’t there. I added it, and now it’s executing the twig, but it’s not displaying the page content, including the response code!

Obviously I just don’t understand the inner workings enough to grok this. I know I’ve been asking tons of questions. What is different in this scenario than in the onPageNotFound scenario? What do I need to do to make this work?

Lots of examples of this in existing plugins. For example in login plugin we commonly replace the current page with one from the plugin:

    public function addRegisterPage()
        $route = $this->config->get('plugins.login.route_register');

        /** @var Pages $pages */
        $pages = $this->grav['pages'];

        $page = new Page;
        $page->init(new \SplFileInfo(__DIR__ . "/pages/"));

        $pages->addPage($page, $route);

Then why might the following code still be giving me a 404?

    public function handleMinimal() {
        dump("I'm here");
        $route = $this->grav['config']->get('plugins.webmention.receiver.route');
        $pages = $this->grav['pages'];
        $page = new Page;
        $page->init(new \SplFileInfo(__DIR__ . '/pages/'));
        $pages->addPage($page, $route);        

The dump displays. The function is tied to onPagesInitialized. It looks to me to be identical to the Login code, which does work (I just installed it to check). I’m not getting any error messages. I can dump($page) and it seems right. I can dump($pages) and the new route appears in the list of routes. But I’m still getting a 404. I even disabled my Graveyard plugin just to make sure there wasn’t some conflict somewhere.

Do you have Grav v1.1.5 installed? We actually made a change/fix in this release to ensure that onPageNotFound is fired after onPageInitialized. Before this version, a not found was often fired before other plugins were able to add their own pages via onPageInitialized ->

According to bin/gpm version I am. I double checked my version of that file and then went back to the markdown and realized that routable was still set to false from when I copied it from the graveyard plugin. sighs Thanks for the help. It appears to be rendering fine now. Baby steps!


I want to return a 201 and inject a Location header in the response. Is this possible?