Silex / Symfony中的路由。提供默认路线 [英] Routing in Silex/Symfony. Providing a default route
问题描述
我正在尝试使用Silex(它使用Symfony路由组件-所以答案也可能适用于Symfony)做某事
I'm attempting to do something using Silex (which uses the Symfony routing component - so the answer may be applicable to Symfony as well)
我要添加Silex到旧版应用程序以提供路由,但是我需要尊重现有应用程序的默认实现来加载文件(这只是从指定URL的文件系统加载文件)。
I am adding Silex to a legacy application to provide routing but I need to respect the existing applications default implementation for loading files (which is simply to load the file from the file system form the URL specified).
编辑:进行澄清:
在进行了一系列自举调用之后,将从文件系统中加载现有文件,作为父模板中的包含文件。
我发现的是,在没有定义的匹配旧页面的路由的情况下,Silex引发了异常。
What I'm finding is that in the absence of a defined route to match the legacy pages, Silex is throwing an exception.
我确实需要一种方法来提供默认的(后备)机制来处理这些旧页面-但是我的模式必须匹配整个url(而不仅仅是一个片段)。
I really need a way to provide a default (fallback) mechanism for handling those legacy pages - but my pattern has to match the entire url (not just one fragment).
这可能吗?
// 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接口一起使用的方法遗留代码(装入/myfolder/mysubfolder/my_php_script.php)
This must be a common use case. I'm trying to provide a way to have a RESTFUL interface alongside legacy code (load /myfolder/mysubfolder/my_php_script.php)
推荐答案
我在symfony食谱中找到了答案...
I found the answer within 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', '.+');
这篇关于Silex / Symfony中的路由。提供默认路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!