# Kirby Pre Controller

**URL:** <https://forum.getkirby.com/t/kirby-pre-controller/3902>\
**Category:** Plugins\
**Created:** [April 19, 2016, 7:57am UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902 "2016-04-19T07:57:58Z")\
**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 19, 2016, 7:57am UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/1 "2016-04-19T07:57:58Z")

</div>

I’ve released yet another plugin. I hope some similar solution will be built into Kirby in the future.

[https://github.com/jenstornell/kirby-pre-controller](https://github.com/jenstornell/kirby-pre-controller)

Require Kirby 2.3 beta.

> # Kirby Pre Controller

1. Let’s say you have a field name called `status` which is set to value `unpublished`.
2. If the value is `unpublished`, redirect the current page to the error page.

> It sounds really simple but it’s quite hard to solve. There are many other cases like this when you may need a pre controller as well.

Maybe it’s the first plugin that uses Page Methods.

---

<div class="post-metadata">

**Author:** ![fvsch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fvsch/32/15908_2.png) [@fvsch](https://forum.getkirby.com/u/fvsch)\
**Post date:** [April 19, 2016, 11:12am UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/2 "2016-04-19T11:12:21Z")

</div>

So basically you need:

1. Code that runs for every request.
2. Which has access the the `Page` object matching the request’s URL.
3. And which can do things like redirecting the page (returning a 301 response) and maybe all kinds of other things.

I’m not sure developers need a helper plugin for that, especially if it means calling that plugin’s code from a snippet. That seems rather brittle.

It looks like developers could just write their own “plugin” script to do whatever they need to do. The main issue is making sure that plugin script has access to the relevant Page object. After a few tries I was able to hack this:

```php
<?php // site/plugins/not-really-a-plugin.php

// page() returns null in this context, but we can retrieve the current path
$path = kirby()->path();

// We should have a page object, or NULL for 404 pages
$page = page( $path ? $path : c::get('home', 'home') );

if ($page) {
   if ($page->status()->value == 'unpublished') {
      go($page->parent()->url());
   }
}

```

There might be ways to return a `410 Gone` response and page, too.

---

<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, 11:28am UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/3 "2016-04-19T11:28:15Z")

</div>

**4. Support for multilanguage urls.**

On a single language setup this solution might be ok. On a multilanguage setup it does not work. When writing `echo $path;` it prints something like this:

```auto
de/not-the-same-url-for-de

```

The plugin has support for multilanguage setup as well.

> It looks like developers could just write their own “plugin” script to do whatever they need to do.

Yes they can! That’s just what I did.

---

<div class="post-metadata">

**Author:** ![fvsch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fvsch/32/15908_2.png) [@fvsch](https://forum.getkirby.com/u/fvsch)\
**Post date:** [April 19, 2016, 11:31am UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/4 "2016-04-19T11:31:12Z")

</div>

> [@jenstornell](#):
>
> If you don’t want to add doctype inside the function you can add it after. Just make sure there are no newlines or space in between. Else some browsers will not render your site correctly.

I don’t think any browser has trouble with whitespace before the doctype. They may have trouble with non-whitespace, and probably with HTML comments, but whitespace is okay.

I would remove the whole doctype handling thing (it’s needless complexity), and just add a note to call the function before the doctype or any HTML content. This is perfectly fine in my experience:

```php
<?php preController(); ?>
<!DOCTYPE html>

```

---

<div class="post-metadata">

**Author:** ![fvsch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fvsch/32/15908_2.png) [@fvsch](https://forum.getkirby.com/u/fvsch)\
**Post date:** [April 19, 2016, 11:41am UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/5 "2016-04-19T11:41:52Z")

</div>

In any case, I would love to have a global controller that is run for all pages and _before_ the template’s controller. Like you, when I read the Kirby docs saying that `controllers/site.php` would only run as a fallback, I was like “damn, that’s too bad”. 😢

---

<div class="post-metadata">

**Author:** ![LCD344](https://avatars.discourse-cdn.com/v4/letter/l/90ced4/32.png) [@LCD344](https://forum.getkirby.com/u/LCD344)\
**Post date:** [April 19, 2016, 11:46am UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/6 "2016-04-19T11:46:43Z")

</div>

Something similar…

is there a way to define and set new filter types? if we could we could use them as middleware before certain routes (or before all of them). Either way, I would like to be able to define new filters.

---

<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, 11:56am UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/7 "2016-04-19T11:56:05Z")

</div>

I’m not sure how all the new browsers handle it, but I’ve had issues with whitespace before the doctype a few years ago myself. Maybe the problem is not a problem anymore.

> Always include one of the doctypes mentioned here as the very first thing in all of your HTML documents.

[https://www.w3.org/wiki/Choosing\_the\_right\_doctype\_for\_your\_HTML\_documents](https://www.w3.org/wiki/Choosing_the_right_doctype_for_your_HTML_documents)

So, I’m not convinced about it yet. I’ll read more about whitespace and think about what approach to take.

> In any case, I would love to have a global controller that is run for all pages and before the template’s controller.

Agree! Then I would love to shut this plugin down. 🙂

@LCD344

You can define [custom collection filters](https://getkirby.com/docs/advanced/filter-methods) or maybe you are talking about something else?

---

<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, 12:14pm UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/8 "2016-04-19T12:14:49Z")

</div>

2 posts were split to a new topic: [How to define route filters](https://forum.getkirby.com/t/how-to-define-route-filters/3906)

---

<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, 12:07pm UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/9 "2016-04-19T12:07:58Z")

</div>

I agree with you here and have opened a [feature request issue](https://github.com/getkirby/kirby/issues/429).

---

<div class="post-metadata">

**Author:** ![fvsch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fvsch/32/15908_2.png) [@fvsch](https://forum.getkirby.com/u/fvsch)\
**Post date:** [April 19, 2016, 12:19pm UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/10 "2016-04-19T12:19:18Z")

</div>

> [@jenstornell](#):
>
> Maybe the problem is not a problem anymore.

It’s not, at least since IE8 (maybe IE6-7 had issues, but I worked on CSS for those browsers too and I don’t remember this issue). Other issues you may have seen are IE 8-10 going in Compatibility mode, but that would be for other reasons (local network, intranets, X-UA-Compatible headers or lack thereof).

---

<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, 12:22pm UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/11 "2016-04-19T12:22:42Z")

</div>

> For me what you just created is a global middleware.

Yes, I guess I did, in your terms. The pre-controller will run exactly before the html is rendered.

> But I can’t find anywhere to define new “filters” (or middleware) for routes… or globally.

Maybe @lukasbestle answered it already.

My biggest problem with the routes is that they run before the `$page` object created. Often I want to do something on the current page, redirect it or do something else depending on some field value or so. For now I’ll do these things with my plugin until something greater is built in, like the feature request issue that @lukasbestle have added.

@fvsch

You sound convincing. I will try modern browsers, including on mobile devices (at least Android) and if it works, maybe remove that ugly string input.

---

<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:** [May 3, 2016, 7:01am UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/12 "2016-05-03T07:01:32Z")

</div>

_ **This plugin has been rewritten and renamed. Now it’s a pure piece of art!** _

[https://github.com/jenstornell/kirby-init-controller](https://github.com/jenstornell/kirby-init-controller)

> [@Kirby Init Controller](https://forum.getkirby.com/t/kirby-init-controller/4038):
>
> Anyone remeber my Kirby Pre Controller? It was a quite poor attempt to solve a real problem. It required extra code in the header.php file. Require Kirby 2.3 beta 2 Init controller [https://github.com/jenstornell/kirby-init-controller](https://github.com/jenstornell/kirby-init-controller) This is an extra controller that will run before the template starts. It works just like any other controller. It runs on every page. It runs after all other controllers has been loaded. The beauty of it is that it’s using the template component which means th…

---

<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:** [May 3, 2016, 8:38am UTC](https://forum.getkirby.com/t/kirby-pre-controller/3902/13 "2016-05-03T08:38:46Z")

</div>


