![]() |
|
sfDomainRoutePlugin - 0.1.3Extend Symfony's routing to allow effective use of domains and subdomains |
|
![]() |
20
users
Sign-in
to change your status |
sfDomainRoutePlugin lets you easily pass parameters in the subdomain (e.g. username.domain.com), limit routes to certain subdomains (e.g. www.domain.com to one route and blog.subdomain.com to another), and generate proper urls between different subdomains. |
The sfDomainRoutePlugin extends Symfony's routing to allow effective use of subdomains and domains.
echo link_to('Blog', '@blog'); /* Will generate a link to http://blog.greenanysite.com/ */ echo link_to('Homepage', '@homepage'); /* Will generate a link to http://www.greenanysite.com/ */ echo link_to('Tal Ater', '@userpage?subdomain=talater'); /* Will generate a link to http://talater.greenanysite.com/ */
In the last example, you can easily retrieve the username in your action with:
$this->username = $request->getParameter('subdomain');
Configuring all of this is very simple:
#Sample route limited to a number of subdomains
homepage:
url: /
class: sfDomainRoute
param: { module: homepage, action: index }
requirements:
sf_host: [www.greenanysite.com, greenanysite.com]
#Sample route limited to one subdomain
blog:
url: /
class: sfDomainRoute
param: { module: blog, action: index }
requirements:
sf_host: blog.greenanysite.com
#Sample route that will capture the subdomain name as a parameter
user_page:
url: /
class: sfDomainRoute
param: { module: user, action: index }
I am also working on adding support for different domain names (and not just subdomains).
This can be very handy for example for pointing multiple domains at the same application and having each one display different data (e.g. www.XboxNews.com, www.PS3News.com, www.WiiNews.com can all easily run from the same application but display different content.)
| Name | Status | |
|---|---|---|
|
|
lead | moc.retalat <<ta>> lat |
Copyright (c) 2012 Tal Ater
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
| Version | License | API | Released |
|---|---|---|---|
| 0.1.3alpha | MIT license | 0.1.0alpha | 18/12/2011 |
| Version | License | API | Released |
|---|---|---|---|
| 0.1.3alpha | MIT license | 0.1.0alpha | 18/12/2011 |
| Version | License | API | Released |
|---|---|---|---|
| 0.1.3alpha | MIT license | 0.1.0alpha | 18/12/2011 |
| 0.1.2alpha | MIT license | 0.1.0alpha | 26/07/2009 |
| 0.1.1alpha | MIT license | 0.1.0alpha | 03/05/2009 |
| 0.1.0alpha | MIT license | 0.1.0alpha | 30/04/2009 |
Enabled support for Symfony 1.3 and 1.4
Enabled support for Symfony 1.3 and 1.4
sfDomainRoute::matchesUrl() no longer returns subdomain parameter if is is empty. For compatibility with $request->getParameter('subdomain', 'default value') thanks: Henrik Bjørnskov
Fixed the way urls are generated. Will now use the host requirements of a route when generating a url if no subdomain defined.
Initial release.
The sfDomainRoutePlugin extends Symfony's routing to allow effective use of subdomains and domains.
Install the plugin
$ symfony plugin:install sfDomainRoutePlugin
Clear your cache
$ symfony cc
Configure your routes.
To access extra features in a route, you must define its class as sfDomainRoute in routing.yml
route_with_subdomain:
url: /
class: sfDomainRoute
param: { module: subdomain, action: index }
To get the subdomain value in your action, simply access it like any other request parameter:
$this->subdomain = $request->getParameter('subdomain');
To limit a route to a specific domain or subdomain, add sf_host to its requirements:
route_with_certain_subdomains:
url: /
class: sfDomainRoute
param: { module: homepage, action: index }
requirements:
sf_host: www.greenanysite.com
If you'd like this route to have more than one domain or subdomain in its requirements, define sf_host as an array:
route_with_certain_subdomains:
url: /
class: sfDomainRoute
param: { module: homepage, action: index }
requirements:
sf_host: [www.greenanysite.com, greenanysite.com]
Generating urls will still work as expected.
Important - While it may be obvious why you need to set routes that use extra domain features, as sfDomainRoute, there is one more limitation that is a bit less obvious. Any routes that you link to from a page with a subdomain must also be of a class sfDomainRoute. No other change is necessary for those routes beyond changing their class.
For example, if you are linking to your download page (www.domain.com/download) from a user page (user.domain.com) you will need to define the route for @download as sfDomainRoute.
The reason for this requirements is that Symfony's own default class for routes will not recognize that the current address is a subdomain and generate either a relative url or an absolute url using the current domain/subdomain, either way the link will still lead to the current subdomain. By defining the linked route as being sfDomainRoute, we can make sure to generate a www.domain.com link for it.
# apps/frontend/config/routing.yml
#Sample route limited to a number of subdomains
homepage:
url: /
class: sfDomainRoute
param: { module: homepage, action: index }
requirements:
sf_host: [www.greenanysite.com, greenanysite.com]
#Sample route limited to one subdomain
blog:
url: /
class: sfDomainRoute
param: { module: blog, action: index }
requirements:
sf_host: blog.greenanysite.com
#Sample route that will capture the subdomain name as a parameter
user_page:
url: /
class: sfDomainRoute
param: { module: user, action: index }
#Sample route that will not receive a subdomain and will default to www.greenanysite.com
install:
url: /install
class: sfDomainRoute
param: { module: install, action: index }
Notice how @homepage is defined before user page. routing.yml is parsed sequentially, so if @user_page was before @homepage it would've captured www.greenanysite.com as a user page with a user name of www.
Notice also that @install is also defined as sfDomainRoute even though it seemingly doesn't use any special domain features. This is done since we'll be linking to this page from a subdomain Otherwise it will default to sfRoute which doesn't recognize the subdomain and we'll end up with user.greenanysite.com/install instead of www.greenanysite.com/install (see Documentation above for a more detailed explanation.)
// apps/frontend/modules/user/actions/actions.class.php $this->username = $request->getParameter('subdomain');
Urls are still generated as expected:
// apps/frontend/modules/user/templates/indexSuccess.php // will generate a link to zzzrbyte.greenanysite.com (works across subdomains. e.g if user is // on talater.greenanysite.com and linking to zzzrbyte.greenanysite.com) echo link_to('zzzrbyte', '@user_page?subdomain=zzzrbyte'); // It even works with POST without specifying method requirements (unlike sfRequestRoute) echo '<form action="'.url_for('@user_page?subdomain=zzzrbyte').'" method="POST">'; // It will also successfully generate a link to www.greenanysite.com/install from the current // subdomain (talater.greenanysite.com) because @install has been defined as sfDomainRoute. echo link_to('install', '@install');
If you find any bugs, have any ideas or just want to talk to me about the plugin, you can use the issue tracker at http://code.google.com/p/sfdomaineouteplugin/issues/list or email me (tal at talater dot com)
sfPropelRoute and sfDoctrineRoute (i.e. sfPropelDomainRoute and sfDoctrineDomainRoute)TODO: Add sf_exclude_subdomain to requirements. Will allow defining a route that won't match on certain subdomains. For example:
user_page:
url: /
class: sfDomainRoute
param: { module: user, action: index }
requirements:
sf_exclude_subdomain: [www, blog]