# How can I use subdomains in routes?

**URL:** <https://discourse.laminas.dev/t/how-can-i-use-subdomains-in-routes/1604>\
**Category:** Mezzio\
**Tags:** mezzio-router\
**Created:** [May 14, 2020, 11:22am UTC](https://discourse.laminas.dev/t/how-can-i-use-subdomains-in-routes/1604 "2020-05-14T11:22:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nepster-web](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/nepster-web/32/432_2.png) [@nepster-web](https://discourse.laminas.dev/u/nepster-web)\
**Post date:** [May 14, 2020, 11:22am UTC](https://discourse.laminas.dev/t/how-can-i-use-subdomains-in-routes/1604/1 "2020-05-14T11:22:28Z")

</div>

So, I have several subdomains for my project:

- [example.com](http://example.com)
- [journal.example.com](http://journal.example.com)
- [account.example.com](http://account.example.com)

How can I use it in routes?

This example does not work:

```
$app->get('journal.example.com', Journal\Handler\HomePageHandler::class, 'journal.home');
```

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [May 14, 2020, 5:17pm UTC](https://discourse.laminas.dev/t/how-can-i-use-subdomains-in-routes/1604/2 "2020-05-14T17:17:00Z")

</div>

Short answer: you can’t.

Longer answer:

Because we wanted a system that would allow consuming any third party router, we had to build abstraction for the lowest common-denominator. In this case, it was path-based routing, as every single routing library you can survey supports this use case. Anything else would require that we potentially provide feature-rich wrappers around other routing solutions, which would completely negate the idea behind having a unified API.

## Workarounds

- You can always supply your own routing and dispatch middleware, giving you complete control over routing.

- For your scenario of subdomain-specific handlers for landing pages, you could have separate vhosts for each, and use `config/autoload/*.local.php` configuration to override dependencies based on the vhost. So, for example, you could map the `HomePageHandler::class` service to a different factory, thus returning a different version (and do the same with template paths).

- You could use middleware between routing and dispatch to change the `RouteResult` based on the detected subdomain (using the current `ServerRequestInterface` to pull this information from its composed `UriInterface`).

---

<div class="post-metadata">

**Author:** ![nepster-web](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/nepster-web/32/432_2.png) [@nepster-web](https://discourse.laminas.dev/u/nepster-web)\
**Post date:** [May 14, 2020, 7:18pm UTC](https://discourse.laminas.dev/t/how-can-i-use-subdomains-in-routes/1604/3 "2020-05-14T19:18:01Z")

</div>

Thank you. I will think how to write my Middleware.

But I still do not understand, why i can’t configure routing libraries, like:

```
->setOptions([
    'subdomain' => 'journal.example.com'
]);
```

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [May 14, 2020, 7:44pm UTC](https://discourse.laminas.dev/t/how-can-i-use-subdomains-in-routes/1604/4 "2020-05-14T19:44:06Z")

</div>

> [@nepster-web](#):
>
> But I still do not understand, why i can’t configure routing libraries, like:
> 
> ```auto
> ->setOptions([
> 'subdomain' => 'journal.example.com'
> ]);
> 
> ```

You _can_ (the `Route` instance returned has that method), but how those options are interpreted is up to the individual router implementations. (`getOptions()`/`setOptions()` exist as the sole mechanism for customizing routes for the specific implementation used).

However, the `Route` instances **are decoupled from the router implementations themselves** , so it’s up to the implementation to decide how to use those options. Currently, none of them support a “subdomain” option, though several support host matching via _other_ mechanisms (not related to options). Since we couldn’t anticipate all the possible options, or how we might want to map them to other features in the various implementations, we restricted ourselves to the lowest common denominator: paths and HTTP methods.

---

<div class="post-metadata">

**Author:** ![nepster-web](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/nepster-web/32/432_2.png) [@nepster-web](https://discourse.laminas.dev/u/nepster-web)\
**Post date:** [May 14, 2020, 8:51pm UTC](https://discourse.laminas.dev/t/how-can-i-use-subdomains-in-routes/1604/5 "2020-05-14T20:51:49Z")

</div>

> [@matthew](#):
>
> You _can_ (the `Route` instance returned has that method), but how those options are interpreted is up to the individual router implementations. ( `getOptions()` / `setOptions()` exist as the sole mechanism for customizing routes for the specific implementation used).
> 
> However, the `Route` instances **are decoupled from the router implementations themselves** , so it’s up to the implementation to decide how to use those options. Currently, none of them support a “subdomain” option, though several support host matching via _other_ mechanisms (not related to options). Since we couldn’t anticipate all the possible options, or how we might want to map them to other features in the various implementations, we restricted ourselves to the lowest common denominator: paths and HTTP methods.

got it. Thank you.  
I will try to resolve this issue and share with the community.
