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
Redirect from HTTP_Exception
  • I don't want to display a message i.e "404 not found" but I want to redirect ont my index page instead (index/index).

    How can I do that from the Http_Exeption_404->get_response() ? I tried but get errors, looks like a return is required and my redirection went wrong.

  • a solution would be wrap Request execution in index.php in try/catch bloc - see here (Kohana 3.1):

    https://github.com/kohana/kohana/blob/3.1/master/index.php#L108

            try
            {
                echo Request::factory()
                  ->execute()
                  ->send_headers()
                  ->body();
            } catch (HTTP_Exception_404 $e)
            {
                Request::current()->redirect('/');
            }
    
  • Thanks. Does the solution is different with 3.3? I'm not sure anymore about the version I use and I don't know how know it. There is nothing about the version in the files, or I couldn't find it.

  • Another solution would be to override HTTP_Exception_404 and redirect from the constructor.

    Are you on Kohana 3.1 or Kohana 3.3? I could not find get_response in 3.1.

  • sorry I posted before reading your last reply.

    There is a constant you can check Kohana::VERSION

  • I'm in 3.3.1, that's why I was not sure ^^

    So the best solution would be to override the construct instead of get_response I guess?

    Thanks.

  • you want to display an error message on the index page?

  • yes, actually on any page. I get the flash errors in my template and display them, the page doesn't matters.

  • if on any, dont throw exception, set $this->responce->status($code)+ add flash message or override Kohana_Exception::handler()

  • Another strange solution would be to HMVC from within HTTP_Exception_404::get_response

    public function get_response() {
        // build and execute main page request
        return Request::factory('/')->execute();
    }
    

    Then you will have all unmatched/unfound urls to show your main page but keep the url at the location bar of the browser.

    beware that this method may cause a loop if you already use HMVC for your main page and if a sub-request results in 404.

  • class HTTP_Exception_404 extends Kohana_HTTP_Exception_404 {

    public function __construct(){
        // Add message to display.
        App::add_flash_message(__('errors.404', array(':controller' => Request::initial()->controller(), ':action' => Request::initial()->action())));
    
        // Redirect to the last visited page.
        App::redirect_last_page(Request::initial()->controller(), Request::initial()->action());
    }
    

    }

    Works perfectly.

    I also tried the return Request::factory('/')->execute();, works too, but I prefer the other way, makes more sens in my app.

    Thanks!

Howdy, Stranger!

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

In this Discussion