How can this be done? I'm trying to do this for about half an hour and it's getting pretty annoying. You would this this should be an basic and easy thing to setup for a framework like this. I hope maybe there's an easy way i missed, because i'm starting to thing i should not chose this framework at all if such basic tings are so hard to setup.
This is in my bootstrap.php file that should do the trick.
if ( ! defined('SUPPRESS_REQUEST'))
{
/**
* Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
* If no source is specified, the URI will be automatically detected.
*/
$request = Request::instance();
try
{
// Attempt to execute the response
$request->execute();
}
catch (Exception $e)
{
if (Kohana::$environment === Kohana::DEVELOPMENT)
{
// Just re-throw the exception
throw $e;
}
echo "ok";
// Create a 404 response
$request->status = 404;
$view = View::factory('error404');
$request->response = $view->render();
}
echo $request->send_headers()->response;
}
But i'm still getting
Fatal error: Uncaught Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI: test ~ SYSPATH\classes\kohana\request.php [ 674 ] thrown in C:\Xampp\htdocs\system\classes\kohana\request.php on line 674
instead of my custom 404 page.
And yes, Kohana::$environment
is set to Kohana::PRODUCTION;
It doesn't even get to the echo "ok";
part. Why doesn't the exception get caught?
Replace the last line of bootstrap.php with:
/**
* Set the production status
*/
define('IN_PRODUCTION', FALSE);
/**
* Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
* If no source is specified, the URI will be automatically detected.
*/
$request = Request::instance();
try
{
$request->execute();
}
catch (Kohana_Exception404 $e)
{
$request = Request::factory('error/404')->execute();
}
catch (Kohana_Exception403 $e)
{
$request = Request::factory('error/403')->execute();
}
catch (ReflectionException $e)
{
$request = Request::factory('error/404')->execute();
}
catch (Kohana_Request_Exception $e)
{
$request = Request::factory('error/404')->execute();
}
catch (Exception $e)
{
if ( ! IN_PRODUCTION )
{
throw $e;
}
$request = Request::factory('error/500')->execute();
}
echo $request->send_headers()->response;
Create new controller "error.php":
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Error extends Controller {
public function action_404()
{
$this->request->status = 404;
$this->request->headers['HTTP/1.1'] = '404';
$this->request->response = 'error 404';
}
public function action_403()
{
$this->request->status = 403;
$this->request->headers['HTTP/1.1'] = '403';
$this->request->response = 'error 403';
}
public function action_500()
{
$this->request->status = 500;
$this->request->headers['HTTP/1.1'] = '500';
$this->request->response = 'error 500';
}
} // End Error
Create two files (exception404.php и exception403.php) in "kohana" folder:
<?php defined('SYSPATH') or die('No direct access');
class Kohana_Exception403 extends Kohana_Exception {
public function __construct($message = 'error 403', array $variables = NULL, $code = 0)
{
parent::__construct($message, $variables, $code);
}
} // End Kohana_Exception 403
<?php defined('SYSPATH') or die('No direct access');
class Kohana_Exception404 extends Kohana_Exception {
public function __construct($message = 'error 404', array $variables = NULL, $code = 0)
{
parent::__construct($message, $variables, $code);
}
} // End Kohana_Exception 404
Now you can manually throw 404 and 403 errors (you can't throw error 500 ;)
throw new Kohana_Exception404;
throw new Kohana_Exception403;