Zend Basic Tutorial
Zend Forms
Zend Database
Zend Advanced
A Hostname Route matches requests based on the host part of the URL (e.g., admin.example.com
, api.example.com
, etc.), not just the path. It’s useful for multi-site, subdomain-based routing, or API versioning.
Use Case |
Hostname Pattern |
Acceptable URL(s) |
---|---|---|
Admin panel |
:subdomain.example.com |
admin.example.com, staff.example.com |
API versioning |
api.:version.example.com |
api.v1.example.com, api.v2.example.com |
Country-based routing |
:country.example.com |
in.example.com, us.example.com |
Wildcard subdomains |
:subdomain.domain.tld |
blog.domain.com, shop.domain.com |
Edit your hosts file
Open your system’s hosts
file:
C:\Windows\System32\drivers\etc\hosts
/etc/hosts
Add this line to simulate a custom domain:
127.0.0.1 admin.localhost
Access your app via browser:
http://admin.localhost:8080/
In your Zend (Laminas) route config, use:
'route' => 'admin.localhost'
'router' => [ 'routes' => [ 'admin' => [ 'type' => \Laminas\Router\Http\Hostname::class, 'options' => [ 'route' => 'admin.localhost', 'defaults' => [ 'controller' => Controller\MainController::class, 'action' => 'dashboard', ], ], 'may_terminate' => true, 'child_routes' => [ 'default' => [ 'type' => \Laminas\Router\Http\Literal::class, 'options' => [ 'route' => '/about', 'defaults' => [ 'controller' => Controller\MainController::class, 'action' => 'about', ], ], ], ], ], ], ],
namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; class MainController extends AbstractActionController { public function dashboardAction() { return new ViewModel(); } public function aboutAction() { return new ViewModel(); } }
File: view/application/main/dashboard.phtml
<div>Dashboard page</div>
File:view/application/main/about.phtml
<h2>About Page</h2> <p>This is the static About page.</p>
URL |
Matched Action |
Output |
---|---|---|
admin.localhost:8080 |
dashboardAction() |
Dashboard page |
admin.localhost:8080/about |
aboutAction() |
About PageThis is the static About page. |