Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

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

Started by Muut Archive 10 years ago · 10 replies · 1113 views
10 years ago

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.

10 years ago

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.

10 years ago

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 :/

10 years ago

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.

10 years ago

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:

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

The markdown file [is here](https://github.com/Perlkonig/grav-plugin-webmention/blob/master/page s/status-update.md). 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?

10 years ago

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

PHP
    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/register.md"));
        $page->template('form');
        $page->slug(basename($route));

        $pages->addPage($page, $route);
    }
10 years ago

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

PHP
    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/status-update.md'));
        $page->template('default');
        $page->slug(basename($route));
        $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.

10 years ago

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!

10 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1362 9 years ago
Archive · by Muut Archive, 9 years ago
2 940 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2959 9 years ago
Archive · by Muut Archive, 9 years ago
3 1124 9 years ago