@@ -72,7 +72,7 @@ use Pecee\SimpleRouter\SimpleRouter;
72
72
73
73
SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\SomeMiddlewareClass'], function() {
74
74
75
- SimpleRouter::group(['prefix' => 'services'], function() {
75
+ SimpleRouter::group(['prefix' => '/ services', 'exceptionHandler' => '\MyProject\Handler\CustomExceptionHandler '], function() {
76
76
77
77
SimpleRouter::get('/answers/{id}', 'ControllerAnswers@show')->where(['id' => '[0-9]+');
78
78
@@ -131,47 +131,57 @@ The framework has it's own ```Router``` class which inherits from the ```SimpleR
131
131
namespace MyProject;
132
132
133
133
use Pecee\Handler\ExceptionHandler;
134
+ use Pecee\SimpleRouter\RouterBase;
134
135
use Pecee\SimpleRouter\SimpleRouter;
135
136
136
137
class Router extends SimpleRouter {
137
138
138
- protected static $exceptionHandlers = array();
139
-
140
- public static function start() {
139
+ protected static $defaultExceptionHandler;
141
140
142
- Debug::getInstance()->add('Router initialised.');
141
+ public static function start($defaultNamespace = null) {
143
142
144
143
// Load routes.php
145
- $file = $_ENV['basePath '] . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'routes.php';
144
+ $file = $_ENV['base_path '] . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'routes.php';
146
145
if(file_exists($file)) {
147
146
require_once $file;
148
147
}
149
148
150
- // Init locale settings
151
- Locale::getInstance();
152
-
153
- // Set default namespace for routes
154
- $defaultNamespace = '\\'.Registry::getInstance()->get('AppName') . '\\Controller';
155
-
156
- // Add custom csrf verifier (must extend BaseCsrfVerifier)
157
- parent::csrfVerifier('MyProject\Middleware\CustomCsrfVerifier');
149
+ // Set default namespace
150
+ $defaultNamespace = '\\'.$_ENV['app_name'] . '\\Controller';
158
151
159
152
// Handle exceptions
160
153
try {
161
154
parent::start($defaultNamespace);
162
155
} catch(\Exception $e) {
163
- /* @var $handler ExceptionHandler */
164
- foreach(self::$exceptionHandlers as $handler) {
165
- $class = new $handler();
166
- $class->handleError($e);
156
+
157
+ $route = RouterBase::getInstance()->getLoadedRoute();
158
+
159
+ $exceptionHandler = null;
160
+
161
+ // Load and use exception-handler defined on group
162
+
163
+ if($route && $route->getGroup()) {
164
+ $exceptionHandler = $route->getGroup()->getExceptionHandler();
165
+
166
+ if($exceptionHandler !== null) {
167
+ $class = new $exceptionHandler();
168
+ $class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
169
+ }
170
+ }
171
+
172
+ // Otherwise use the fallback default exceptions handler
173
+
174
+ if(self::$defaultExceptionHandler !== null) {
175
+ $class = new self::$defaultExceptionHandler();
176
+ $class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
167
177
}
168
178
169
179
throw $e;
170
180
}
171
181
}
172
182
173
- public static function addExceptionHandler ($handler) {
174
- self::$exceptionHandlers[] = $handler;
183
+ public static function setDefaultExceptionHandler ($handler) {
184
+ self::$defaultExceptionHandler = $handler;
175
185
}
176
186
177
187
}
0 commit comments