我有一个使用Symfony2的网站,我想有一个完全不同的路由文件,这取决于用户(ip地址,…)
我的第一个想法是加载一个不同的环境如果用户的功能,但内核(所以环境设置)是在事件之前设置的,我认为这个解决方案不能工作。
我想保持相同的url,不重定向到另一个网站…
如果你有任何想法,谢谢:)
您可以创建额外的加载器,它将像文档中那样扩展现有的加载器。在你的例子中:
<?php
namespace AppBundle'Routing;
use Symfony'Component'Config'Loader'Loader;
use Symfony'Component'HttpFoundation'Request;
use Symfony'Component'Routing'RouteCollection;
class AdvancedLoader extends Loader
{
private $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function load($resource, $type = null)
{
$collection = new RouteCollection();
$ip = $this->request->getClientIp();
if($ip == '127.0.0.1'){
$resource = '@AppBundle/Resources/config/import_routing1.yml';
}else{
$resource = '@AppBundle/Resources/config/import_routing2.yml';
}
$type = 'yaml';
$importedRoutes = $this->import($resource, $type);
$collection->addCollection($importedRoutes);
return $collection;
}
public function supports($resource, $type = null)
{
return 'advanced_extra' === $type;
}
}
<标题> AppBundle/资源/config/services.yml h1> 标题> app/config/routing.yml h1> div class="answers">
你可以使用一个PHP文件作为你的主路由器,然后根据你的条件(用户,IP,…),你可以加载动态路由或加载单独的路由文件。
通过http://symfony.com/doc/current/book/routing.html你可以像这样设置你的路由:
# app/config/config.yml
framework:
# ...
router: { resource: "%kernel.root_dir%/config/routing.php" }
在routing.php文件中,你可以导入静态文件(yml, xml)或直接在那里注册路由(所有这些都基于你的特定条件)。
标题>