@@ -57,7 +57,8 @@ function __construct($params = [])
5757 if (is_null ($ params ) || empty ($ params ) || !is_array ($ params ))
5858 return ;
5959
60- RouterException::$ debug = (isset ($ params ['debug ' ]) && $ params ['debug ' ] === true ? true : false );
60+ if (isset ($ params ['debug ' ]) && is_bool ($ params ['debug ' ]))
61+ RouterException::$ debug = $ params ['debug ' ];
6162
6263 if (isset ($ params ['paths ' ]) && $ paths = $ params ['paths ' ])
6364 {
@@ -160,19 +161,25 @@ public function pattern($pattern, $attr = null)
160161 if (!in_array ('{ ' . $ key . '} ' , array_keys ($ this ->patterns )))
161162 $ this ->patterns ['{ ' . $ key . '} ' ] = '( ' . $ value . ') ' ;
162163 else
163- return new RouterException ($ key . ' pattern cannot be changed. ' );
164+ return $ this -> exception ($ key . ' pattern cannot be changed. ' );
164165 }
165166 else
166167 {
167168 if (!in_array ('{ ' . $ pattern . '} ' , array_keys ($ this ->patterns )))
168169 $ this ->patterns ['{ ' . $ pattern . '} ' ] = '( ' . $ attr . ') ' ;
169170 else
170- return new RouterException ($ pattern . ' pattern cannot be changed. ' );
171+ return $ this -> exception ($ pattern . ' pattern cannot be changed. ' );
171172 }
172173
173174 return ;
174175 }
175176
177+
178+ /**
179+ * Add new middleware
180+ *
181+ * @return
182+ */
176183 public function middleware ($ name , $ command )
177184 {
178185 $ this ->middlewares [$ name ] = $ command ;
@@ -273,7 +280,7 @@ public function run()
273280 $ this ->errorCallback = function ()
274281 {
275282 header ($ _SERVER ['SERVER_PROTOCOL ' ]." 404 Not Found " );
276- return new RouterException ( ' Bad Request :( Looks like something went wrong. Please try again. ' );
283+ return $ this -> exception ( ' Route not found. Looks like something went wrong. Please try again. ' );
277284 };
278285 }
279286
@@ -342,7 +349,7 @@ public function controller($route, $controller)
342349 $ req = require ($ controllerFile );
343350 }
344351 else
345- return new RouterException ($ controller . " controller file is not found! Please, check file. " );
352+ return $ this -> exception ($ controller . " controller file is not found! Please, check file. " );
346353
347354 $ classMethods = get_class_methods ($ this ->namespaces ['controllers ' ] . $ controller );
348355
@@ -510,4 +517,14 @@ public function getRoutes()
510517 {
511518 return $ this ->routes ;
512519 }
520+
521+ /**
522+ * Throw new Exception for Router Error
523+ *
524+ * @return RouterException
525+ */
526+ public function exception ($ message = '' )
527+ {
528+ return new RouterException ($ message );
529+ }
513530}
0 commit comments