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
Environment and error 404
  • Okay, I'm trying to get my live site (I also have a dev site) to show error 404 pages instead of the error exception screen.

    Firstly how do I know if my site is in DEVELOPMENT or PRODUCTION environment?

    Secondly how do I go about changing it to PRODUCTION environment?

    Thanks

  • Firstly how do I know if my site is in DEVELOPMENT or PRODUCTION environment?

    Kohana::$environment is the way to go.

    Secondly how do I go about changing it to PRODUCTION environment?

    Look at the note at the end of the Initialization and Configuration: just set $_ENV['KOHANA_ENV'].

  • I have this line at the top of my .htaccess file

    SetEnv KOHANA_ENV development
    

    Of course, on the production server, it's production

    More here: http://forum.kohanaframework.org/discussion/comment/59027


    @zombor, could we please have the Cloudflare's caching disabled for the forum's rss feed? Thanks :)

  • "Kohana::$environment is the way to go." Am I echoing this? If so I get "40".

    "just set $_ENV['KOHANA_ENV']." Could you elaborate on this a little. How come this is not in the bootstrap file to start with?

  • Thanks for the responses btw Darsstar and enov

  • Kohana has four predefined values you can choose to use. You don't have to of course, but Kohana::PRODUCTION, Kohana::STAGING, Kohana::TESTING and Kohana::DEVELOPMENT probably suffice for a lot of people. They are numbers so you can do this:

    if (Kohana::$environment <= Kohana::TESTING) {
        ...
    }
    

    The reason they have the values 10, 20, 30 and 40 is so that additional environments can be added in between.

    If you want to display "development" on the screen, then it depends on how you set Kohana::$environment. If using the suggested method your human readable value is stored in $_ENV['KOHANA_ENV']. If not, then you choose to do it your own way. So come up with your own way to display a human readable value.

    Could you elaborate on this a little. How come this is not in the bootstrap file to start with?

    I did point you to the following note in the userguide:

    Note: The default bootstrap will set Kohana::$environment = $_ENV['KOHANA_ENV'] if set. Docs on how to supply this variable are available in your web server's documentation (e.g. Apache, Lighttpd). This is considered better practice than many alternative methods to set Kohana::$enviroment, as you can change the setting per server, without having to rely on config options or hostnames.

    Although this is slightly outdated, which I should have noticed before. The actual code in the bootstrap is this:

    /**
    * Set Kohana::$environment if a 'KOHANA_ENV' environment variable has been supplied.
    *
    * Note: If you supply an invalid environment name, a PHP warning will be thrown
    * saying "Couldn't find constant Kohana::<INVALID_ENV_NAME>"
    */
    if (isset($_SERVER['KOHANA_ENV']))
    {
        Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
    }
    

    So to correct myself: set $_SERVER['KOHANA_ENV'].

  • Thanks Darsstar managed to get "10" or PRODUCTION by inserting this line "Kohana::$environment = Kohana::PRODUCTION;" in the bootstrap.php file above the script you posted above.

    How do I now use this environment to show an error 404 instead of the developer exception screen?

  • Read this tutorial: http://kohanaframework.org/3.3/guide/kohana/tutorials/error-pages Maybe the entire userguide, doesn't hurt :p

    That is for HTTP_Exception's for the rest overwrite Kohana::response()

  • Thanks Darsstar.

  • Any ideas how I can get my template around the view/error.php file?

            $view = View::factory('error');
    
            $response = Response::factory()
                ->status($this->getCode())
                ->body($view->render());
    
            return $response;
    
  • @argybee

    class HTTP_Exception_404 extends Kohana_HTTP_Exception_404 {
     
        public function get_response()
        {
            $view = View::factory('error');
            $view->message = $this->getMessage();
            $view->code = $this->getCode();
     
            $response = Response::factory()
                ->status(404)
                ->body($view->render());
     
            return $response;
        }
    }
    

    views/error.php:

    ...
    <?php echo View::factory('errors/'.$code, array('message' => $message)) ?>
    ..
    
  • Sorry WS should have been more specific. Rather than having to create a template from scratch in 'my_template.php' I want to use pre-existing elements (header/footer etc.) similar to other controller files that use:

    $this->template->content = $view;

  • @argybee create controller for render your template, call him in Exception controller and add into rendered error

  • I've created a controller called 'Exception.php' but how does the class display the view via the controller? Have I misunderstood?

  • @argybee create controller HTTP_Error.php extends Your_Basic_Controller:

    $view = View::factory('error', $this->request->query());
    $this->template->content = $view->render();
    

    in HTTP_Exception:

    $content = Request::factory('http/error')
          ->query(array('message' => $this->getMessage(), 'code' => $this->getCode()))
          ->execute();
     
    $response = Response::factory()
          ->status($this->getCode())
        ->body($content);
     
    return $response;
    
  • @WinterSilence I get the following:

    "Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 72 bytes) in /####/####/public_html/development/system/classes/Kohana/HTTP/Exception.php on line 17"

  • You can just:

            $view = View::factory('error');
            $template = View::factory('template');
            $template->content = $view;
    
            $response = Response::factory()
                ->status($this->getCode())
                ->body($template->render());
    
            return $response;
    
    
  • Looks like enov has got the solution I was looking for. Thank you both for your help. Much appreciated.

  • @WinterSilence it was this bit of code that did it:

    $template = View::factory('template'); $template->content = $view;

    Apologies if you had already implied that. Must have missed it. Once again thanks for your help.

  • True way:

    class HTTP_Exception extends Kohana_HTTP_Exception
    {
        public function get_response()
        {
            // Lets log the Exception, Just in case it's important!
            Kohana_Exception::log($this);
    
            if (Kohana::$environment >= Kohana::DEVELOPMENT)
            {
                // Show the normal Kohana error page.
                return parent::get_response();
            }
    
            $query = array('code' => $this->getCode(), 'message' => $this->getMessage());
            $content = Request::factory('error')->query($query)->execute();
    
            $response = Response::factory()
                ->status($this->getCode())
                ->body($content);
    
            return $response;
        }
    }
    
    class Controller_Page_Error extends Controller_Page
    {
        public function action_index()
        {
            $this->content->message = $this->request->query('message');
            $this->content->code = $this->request->query('code');
        }
    }
    
    < h3 >Error <?php echo $code ?></ h3 >
    < div ><?php echo $message ?></ div >
    
  • @WinterSilence,

    I like the controller way better.

    However, in your last example, Controller_Page_Error should test for HMVC and throw HTTP 404 if the request it received is not HMVC.

    Otherwise, it could be available from the client (browser address bar), and the view is susceptible to XSS attack.

    I believe you had thrown your 2c. However, it's better to fix as it might serve as example :)

    Cheers!

  • @enov this is an example, not a ready solution. HTTP_Exception throw HTTP_Exception_404? i seem to be looping.

  • @WinterSilence,

    HTTP_Exception throw HTTP_Exception_404? i seem to be looping.

    lol... you're right.

    But then again, would it not break the loop and continue, once it happen to be a sub request?

    Good point, though.

  • @WinterSilence, @argybee

    What happens when the error comes from the site template? or from the pre-existing elements header/footer? Or from the base controller?

    a loop? a mix of Kohana error views and custom views?

    I am changing my mind regarding this matter:

    Error views should be VERY simple and should live on its own. It should not depend on anything.

  • I am sorry to pollute this area.

    Could we say it's safer to use pre-existing views/templates/base controllers in case of HTTP_Exception_404? And use a simple error view in case of HTTP_Exception_500?

    My comment above probably would better make sense for HTTP_Exception_500 :)

    I am working now to implement custom error pages and my ideas are getting mixed. Perhaps someone wiser would shed some light.

  • write subblocks errors in log , display (throw exceptions) in main content

Howdy, Stranger!

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

In this Discussion