# Return error page if field value is something

**URL:** <https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894>\
**Category:** Questions\
**Tags:** v2\
**Created:** [April 18, 2016, 9:32am UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894 "2016-04-18T09:32:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [April 18, 2016, 9:32am UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/1 "2016-04-18T09:32:39Z")

</div>

I often have kind of the same problem. Here I have a tiny code that does not work.

How do I return/visit the error page if (in this case) the field key `status` has the value `unpublished`?

```php
kirby()->routes(array( 
  array(
    'pattern' => '(.+)', 
    'action' => function() {
      if( $page->status === 'unpublished' ) {
        return site()->visit('error');
      }
    }
  ),
));

```

**Lessons learned:**

- `$page` is not passed to the route. Route is inited before the `$page` is set.
- The `(:all)` don’t work. [368](https://github.com/getkirby/kirby/issues/368).

Be creative!

---

<div class="post-metadata">

**Author:** ![flokosiol](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/flokosiol/32/468_2.png) [@flokosiol](https://forum.getkirby.com/u/flokosiol)\
**Post date:** [April 18, 2016, 9:47am UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/2 "2016-04-18T09:47:49Z")

</div>

What about using a shared controller for all pages and do the magic there?

> [@Shared controller](https://forum.getkirby.com/t/shared-controller/1839):
>
> A controller is always attached to a template. I needed to have the same code in three different controllers. I felt bad about duplicating the code. It also felt strange to create a plugin and jump between the controller and the plugin. Shared controller In the controllers where you have duplicated code, (for example /site/controllers/home.php) do this: require\_once kirby()-\>roots()-\>controllers() . '/shared/my-shared-controller.php'; Try it out In /site/controllers/shared/my-shared-controll…

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 18, 2016, 9:50am UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/3 "2016-04-18T09:50:19Z")

</div>

This should work:

```auto
kirby()->routes(array(
  array(
    'pattern' => '(.+)',
    'action' => function($uri) {
      $page = page($uri);
      if( $page->status()->value() === 'unpublished' ) {
        return site()->visit('error');
      }
      else {
          return site()->visit($page);
      }
    }
  ),
));

```

Edit: I added an else statement.

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [April 18, 2016, 10:40am UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/4 "2016-04-18T10:40:36Z")

</div>

@flokosiol

Interesting that you reply to my topic with my own answer. 🙂 Maybe that could be a solution in the future.

[getkirby/kirby/issues/423](https://github.com/getkirby/kirby/issues/423)

@texnixe

That does only work for single language sites. Else it will not match. I have found that it’s possible to get the “original” url in the server variable, like this:

```php
print_r( substr( $_SERVER['HTTP_REFERER'], 0, -5 ) );

```

I have not found a way to remove the stuff in the beginning safely enough. I also don’t know how reliable it is.

**For me it returns:**

```auto
http://localhost/status/panel/pages/projects

```

So, no good enough solution yet. More ideas are welcome.

## Update

The `$_SERVER['HTTP_REFERER']`only seems to be active when logged in to the panel so that solution is out of the question.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 18, 2016, 10:47am UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/5 "2016-04-18T10:47:24Z")

</div>

Yes, you are right, multi-lang sites and routes are difficult: [Routes in multi-lang setup](https://forum.getkirby.com/t/routes-in-multi-lang-setup/3487)

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [April 18, 2016, 12:36pm UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/6 "2016-04-18T12:36:23Z")

</div>

I tried your suggestion a little bit more because it’s still possible to use a site controller in 2.3, just not with a plugin yet.

```php
<?php
return function($site, $pages, $page) {
  $errorPage = site()->errorPage();
  echo site()->visit( $errorPage->uri() );
  die;
};

```

I did not get the visit function to work. Maybe it will only work inside a route? It output `error` instead of the template.

```php
// Loads the error page with default template
return function($site, $pages, $page) {
  $page = site()->errorPage();
  $template = tpl::load(
    kirby()->roots()->templates() . DS . 'default.php',
      array(
        'site' => $site,
        'pages' => array(),
        'page' => $page,
      )
    );
  echo $template;
  die;
}

```

I also tried to load a template, but because it loses the variables in the snippets it does not work.

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [April 18, 2016, 1:29pm UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/7 "2016-04-18T13:29:23Z")

</div>

## Workaround

The closest thing I have for a solution is a `site.php` controller with a `header::redirect`.

It redirects to the error page. Not perfect but the best I could come up with.

```php
<?php
return function($site, $pages, $page) {
  if( $page->status()->value() === 'unpublished') {
    $errorPage = site()->errorPage();
      if( $page->uri() != $errorPage->id() ) {
        header::redirect($errorPage->url(), 301);
      }
  }
};

```

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [April 18, 2016, 4:41pm UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/8 "2016-04-18T16:41:18Z")

</div>

@lukasbestle

You set this to solved. You think this is the best solution right now then?

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [April 18, 2016, 4:54pm UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/9 "2016-04-18T16:54:04Z")

</div>

There’s always a better solution, but I think yours is just fine. It’s a more elegant equivalent of putting:

```php
<?php if($page->status() == 'unpublished') { go('error'); die(); } ?>

```

in the first line of the template.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 18, 2016, 5:39pm UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/10 "2016-04-18T17:39:13Z")

</div>

Do you want this to apply to all pages of the site? The default controller is only a fallback if there is no specific page controller. So wouldn’t it then make more sense to put that bit of code into the header snippet?

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [April 19, 2016, 5:55am UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/11 "2016-04-19T05:55:26Z")

</div>

@lukasbestle

I don’t know if it would work if the `error` config is changed?

`go` can not be used with a redirect code, but I hope that will change. I even added an issue for it [160](https://github.com/getkirby/toolkit/issues/160).

@texnixe

I thought when we finally have a global controller `site.php` we could use that, but as you say it’s just a fallback and it would still not be nice if we have 10 controllers. The problem remains.

The header snippet still seems to be the only good option in this case.

Thank you both!

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [April 19, 2016, 8:09am UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/12 "2016-04-19T08:09:11Z")

</div>

Well, you could always use `go($site->errorPage())`, but normally the error option won’t be changed later on, so I haven’t yet needed that.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:59pm UTC](https://forum.getkirby.com/t/return-error-page-if-field-value-is-something/3894/13 "2025-02-06T16:59:46Z")

</div>


