I'm trying to create a default route in my Laravel 4 REST API, that hits, when none of my other defined routes matches the request, to return a specific error to the caller.
Is this possible? Unfortunately, I found nothing in the docs, so I played around and tried using a wildcard (*
) as the last Route definition in my routes.php
but that doesn't work.
Route::when("*", function(){
throw new CustomizedException('Route not found...');
});
When I have this route and do an artisan routes
, I get an Exception:
{"error":{"type":"ErrorException","message":"Object of class Closure could not be converted to string","file":"\/Applications\/MAMP\/htdocs\/CampaigningTools\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Console\/RoutesCommand.php","line":153}}
Calling an inexistent route does not trigger my customized Exception, but the standard one:
{"error":{"type":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException","message":"","file":"\/Applications\/MAMP\/htdocs\/CampaigningTools\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php","line":1429}}
I also tried using any
as suggested in this post, but that also doesn't work:
Route::any( '(.*)', function( $page ){
throw new ValidationException('Custom error');
});
This route also doesn't fire, when I call an inexistent route.
Any hints, what I'm doing wrong would be appreciated.