Routing in Silex / Symfony. Providing a default route

I am trying to do something using Silex (which uses the Symfony routing component - so the answer might apply to Symfony too)

I am adding Silex to an outdated application for providing routing, but I need to respect the existing default application implementation for downloading files (this is just downloading a file from the file system with the specified URL).

edit: to clarify: An existing file is downloaded from the file system as included in the parent template after a series of bootstrap calls have been completed.

What I find is that in the absence of a specific route for matching outdated pages, Silex throws an exception.

I really need a way to provide a default (fallback) mechanism to handle these old pages, but my template should match the entire URL (and not just one snippet).

Is it possible?

// Include Silex for routing require_once(CLASS_PATH . 'Silex/silex.phar'); // Init Silex $app = new Silex\Application(); // route for new code // matches for new restful interface (like /category/add/mynewcategory) $app->match('/category/{action}/{name}/', function($action, $name){ //do RESTFUL things }); // route for legacy code (If I leave this out then Silex // throws an exception beacuse it hasn't matched any routes $app->match('{match_the_entire_url_including_slashes}', function($match_the_entire_url_including_slashes){ //do legacy stuff }); $app->run(); 

This should be a common precedent. I am trying to provide a way to have a RESTFUL interface along with legacy code (load / myfolder / mysubfolder / my_php_script.php)

+8
symfony routing silex
source share
2 answers

I found the answer in the symfony cookbook ...

http://symfony.com/doc/2.0/cookbook/routing/slash_in_parameter.html

 $app->match('{url}', function($url){ //do legacy stuff })->assert('url', '.+'); 
+28
source share

You can use error handling using something like this:

 use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; $app->error(function (\Exception $e) use ($app) { if ($e instanceof NotFoundHttpException) { return new Response('The requested page could not be found. '.$app['request']->getRequestUri(), 404); } $code = ($e instanceof HttpException) ? $e->getStatusCode() : 500; return new Response('We are sorry, but something went terribly wrong.', $code); }); 
+4
source share

All Articles