Reputation: 10089
I have a website using Symfony2 and I'd like to have a completely different routing file depending of the user (ip address, ...)
My first idea was to load a different environment if function of the user, but the kernel (so the environment setup) is set before the events, I think this solution can't work.
I want to keep the same url, no redirection on another website ...
If you have any idea, thanks :)
Upvotes: 3
Views: 330
Reputation: 786
You can create extra loader which will extend your existing loaders like in documentation. In your case:
<?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;
}
}
services:
app.routing_loader:
class: AppBundle\Routing\AdvancedLoader
arguments: [@request=]
tags:
- { name: routing.loader }
app_advanced:
resource: .
type: advanced_extra
Upvotes: 4
Reputation: 111
You could use a PHP file as your main router and then depending on your conditions (user, IP, ...) you could load dynamic routes or load individual routing files.
Going by http://symfony.com/doc/current/book/routing.html you could set up your routing like this:
# app/config/config.yml
framework:
# ...
router: { resource: "%kernel.root_dir%/config/routing.php" }
In the routing.php file you can import static files (yml, xml) or just register the routes directly there (all of this based on your specific conditions).
Upvotes: 0