Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added subdomain matching capability. #52

Closed
wants to merge 1 commit into from

Conversation

PhilLehmann
Copy link

If you'd like, feel free to pull this small change.

While the change is not backwards compatible due to newly introduced method parameters, adding them at the end of the parameter list didn't seem sensible 👿

Thanks!

-phil

@niahoo
Copy link
Collaborator

niahoo commented Feb 5, 2014

Hi,

I don't understand why you just simply work on a hostname level, why subdomains ?

I think there's too much changes to the API. Plus AltoRouter generates absolute paths, you can just prefix urls with your host of choice.

@koenpunt
Copy link
Collaborator

koenpunt commented Feb 6, 2014

A backwards compatible solution could be adding additional methods for subdomain routing instead of modifying the existing ones.

@niahoo
Copy link
Collaborator

niahoo commented Mar 7, 2014

To me it's simple : If you don't want a route to be matched on a specific (sub)domain, you don't map it.

If the router doesn't even know a route it would discard anyway, it's even cheaper.

If you create a router and save it on APC cache, then you should create a router per subdomain. But if you have dynamic subdomains you first check wether the subdomain is someuser.host.com or simply host.com and pick the good router accordingly.

@koenpunt
Copy link
Collaborator

I see value in constraints on routes when they can be defined separate from them.

A format I like (and in someway use in my own implementation) is as follows:

$router->constraint(array('host' => 'example.com'), function($router) {
  $router->map('GET', '/', function() {
    echo "Root on example.com";
  });
});

Or even more flexible, with a generic function:

$router->constraint(function() {
  return $_SERVER['HTTP_HOST'] == 'example.com';
}, function($router) {
  $router->map('GET', '/', function() {
    echo "Root on example.com";
  });
});

And this can even be a function not in the library. Take for example:

function constraints($router, $constraint, $callback) {
  if($constraint()) {
    $callback($router);
  }
}

constraint($router, function() {
  return $_SERVER['HTTP_HOST'] == 'example.com';
}, function($router) {
  $router->map('GET', '/', function() {
    // Home on example.com
  });
  $router->map('GET', '/page/[i:id]', function() {
    // Page on example.com
  });
})

The concepts laid out here above can also be applied for namespace (#149) and route groups (#83).

Because this is a returning topic, I've opened an issue to discuss the requirements for such functionality: #156

@koenpunt koenpunt closed this Jun 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants