我正在尝试使用Silex(它使用Symfony路由组件 - 做一些事情 - 所以答案也可能适用于Symfony)
我正在将Silex添加到遗留应用程序以提供路由,但我需要尊重现有应用程序的默认实现以加载文件(这只是从文件系统加载文件形式指定的URL)。
编辑:澄清: 在进行了一系列引导调用之后,现有文件作为父模板中的包含从文件系统加载。
我发现,如果没有定义的路由来匹配遗留页面,Silex就会抛出异常。
我真的需要一种方法来提供处理这些遗留页面的默认(回退)机制 - 但我的模式必须匹配整个URL(而不仅仅是一个片段)。
这可能吗?
// 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();
这必须是一个常见的用例。我试图提供一种方法来与遗留代码一起使用RESTFUL接口(load /myfolder/mysubfolder/my_php_script.php)
答案 0 :(得分:28)
我在symfony食谱中找到了答案......
http://symfony.com/doc/2.0/cookbook/routing/slash_in_parameter.html
$app->match('{url}', function($url){
//do legacy stuff
})->assert('url', '.+');
答案 1 :(得分:4)
您可以使用类似的错误处理:
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);
});