Symfony2 Routing - route subdomains
This is my solution:
In the config.yml
inside app dir add the following lines:
services:
kernel.listener.subdomain_listener:
class: Acme\DemoBundle\Listener\SubdomainListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onDomainParse }
Then create the class SubdomainListener.php
as:
<?php
namespace Acme\DemoBundle\Listener;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
class SubdomainListener
{
public function onDomainParse(Event $event)
{
$request = $event->getRequest();
$session = $request->getSession();
// todo: parsing subdomain to detect country
$session->set('subdomain', $request->getHost());
}
}
Symfony sub domain routing
This solution will intercept the REQUEST_URI and add the subdomain as a root folder if not already used.
Meaning app.example.com and app.example.com/app will both access the same page.
if(substr($_SERVER['HTTP_HOST'], 0, strlen('app.')) === 'app.'
&& substr($_SERVER['REQUEST_URI'], 0, strlen('/app')) !== '/app')
{
$_SERVER['REQUEST_URI'] = '/app'.$_SERVER['REQUEST_URI'];
}
The benefit of this is being able to put all your controllers in folders. If you had a multiple profile controllers, they could both be accessed from /profile but under different sub domains.
In Symfony2 routing, how do I setup an optional subdomain
The only way I could get this to work was to use different types of routing.
app/config/routing.yml
_main:
resource: routing.yml
incompass_sterilization:
host: spd.casechek-dev.com
resource: "@IncompassSterilizationBundle/Resources/config/routes.yml"
incompass_printing:
host: labels.casechek-dev.com
resource: "@IncompassPrintingBundle/Resources/config/routes.yml"
incompass_admin:
host: admin.casechek-dev.com
resource: "@IncompassAdminBundle/Resources/config/routes.yml"
incompass_web:
host: www.casechek-dev.com
resource: "@IncompassWebBundle/Resources/config/routes.yml"
incompass_web_two:
host: casechek-dev.com
resource: "@IncompassWebBundle/Resources/config/routes_two.yml"
IncompassWebBundle/Resources/config/routes.yml
incompass_web_main:
type: annotation
prefix: /
resource: Incompass\WebBundle\Controller\MainController
IncompassWebBundle/Resources/config/routes_two.yml
incompass_web_main_two:
path: /
defaults: { _controller: IncompassWebBundle:Main:index }
Symfony2 Subdomain Routing - Different Bundles
There is a discussion going on to add this feature.
Symfony2 multiple config and routing files for subdomain routing
I had the same need so we did like this:
/apps/config
/apps/config/common_config.yml
/apps/config/common_routing.yml
/apps/config/...
/apps/myapp1
/apps/myapp1/myapp1Kernel.php
/apps/myapp1/...
/apps/myapp1/config
/apps/myapp1/config/config.yml
/apps/myapp1/config/routing.yml
/apps/myapp1/config/...
/apps/myapp2
/apps/myapp2/myapp1Kernel.php
/apps/myapp2/...
/apps/myapp2/config
/apps/myapp2/config/config.yml
/apps/myapp2/config/routing.yml
/apps/myapp2/config/...
...
And in each app's yml file, we had:
/apps/myapp1/config/config.yml
imports:
- { resource: "../../config/common_config.yml" }
And then, you have to reproduce the same way in /web
/web/myapp1/app.php
Who will be calling your app
$kernel = new myapp1Kernel('prod', false);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
Related Topics
PHP Password_Verify() Hash and Pass Won't Match
Fastest Hash for Non-Cryptographic Uses
Laravel Migration Table Already Exists, But I Want to Add New Not the Older
Codeigniter Check for User Session in Every Controller
How to Add Square Braces Around Subarray Data Inside of a JSON Encoded String
Column Count of MySQL.Proc Is Wrong. Expected 20, Found 16. the Table Is Probably Corrupted
Sending Xml Data Using Http Post with PHP
How to Create an Empty Array in PHP with Predefined Size
How to Access JSON Decoded Array in PHP
Unknown Modifier '/' in ...? What Is It
Doctrine - How to Print Out the Real SQL, Not Just the Prepared Statement
Why Are PHP Function Calls *So* Expensive
How to Alias the Name of a Column in Eloquent
Search and Replace Multiple Values with Multiple/Different Values in PHP5