TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Where can I write code executed for each request?
  • I need to add some code that will be executed for each HTTP request as soon as possible.

    Basically, I want to make a FLASH system about mesages which are deleted between each HTTP requests. (used for error messages, especially!)

    But I can't figure out where write it, because I tried in my application/classes/Controller/Template.php, but it's not called when the controller is not found so the flash messages are not reset.

    Then I tried in application/classes/Controller.php:

    abstract class Controller extends Kohana_Controller {

    public function __construct(Request $request, Response $response){
        $session = Session::instance();
        // Clear all flash data.
        if (!$session->get_once(App::config('controller.key_preserve_flash'), false)){
        parent::__construct($request, $response);


    But still the same, the Controller::construct is called only when a controller is found.

    Help me to figure this out, please. i'm thinking about Request class, but ...

  • Why not in bootstrap.php just before your routes?

  • That should not belongs there but that could be a temporary solution.

  • add $this-request->url()(or uri() ?!) to key_preserve_flash and check in controller as: if exist $this->request->referrer()+key_preserve_flash, then display this message.

  • The point is that the Controller won't be called if the targeted route doesn't use any existing controller. It works fine with my code when the controller actually exists, but doesn't when it's a 404, for instance. Works fine too when the controller exists, but not the method.

  • @vadorequest then add this code in session constructor and instance in bootstrap or add in kohana::init

  • Session constructor sounds good for me. I'll try tonight. Thanks.

  • Session constructor is not a good idea, as you may call it twice or event more in a single request.

    place your code in your_module_dir/init.php

  • I don't think so, because the Session class is a singleton and we have to use Session::instance() to get the current instance, not a new Session()

  • just do it, if you like that. but I think init.php is best practice for your case

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion