Zend Basic Tutorial
Zend Forms
Zend Database
Zend Advanced
Routing in Zend Framework is the process of mapping a URL to a specific controller and action.
For example:
http://example.com/user/profile
Can be routed to:
UserController
profileAction()
Routes are defined inside:
When a user visits /
, Zend loads MainController::homeAction()
and shows the view in view/application/main/home.phtml
.
namespace Application; use Laminas\Router\Http\Literal; use Application\Controller\MainController; use Laminas\ServiceManager\Factory\InvokableFactory; return [ 'router' => [ 'routes' => [ 'home' => [ 'type' => Literal::class, 'options' => [ 'route' => '/', 'defaults' => [ 'controller' => MainController::class, 'action' => 'home', ], ], ], ], ], 'controllers' => [ 'factories' => [ MainController::class => InvokableFactory::class, ], ], 'view_manager' => [ 'template_path_stack' => [ __DIR__ . '/../view', ], 'template_map' => [ 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', ], ], ];
'router'
– Routing Configuration (module/Application/config/module.config.php)'router' => [ 'routes' => [ 'home' => [ 'type' => Literal::class, 'options' => [ 'route' => '/', 'defaults' => [ 'controller' => Controller\MainController::class, 'action' => 'home', ], ], ], ], ],
home
/
(homepage)MainController::homeAction()
'controllers'
– Registering Controllers (module/Application/config/module.config.php)'controllers' => [ 'factories' => [ Controller\MainController::class => InvokableFactory::class, ], ],
MainController
InvokableFactory
since the controller has no constructor parametersMainController.php (module\Application\src\Controller\MainController.php)
<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; class MainController extends AbstractActionController { public function homeAction() { return new ViewModel(); } }
Action
is an action.homeAction()
→ URL: /
'view_manager'
– View Folder Setup (module/Application/config/module.config.php)'view_manager' => [ 'template_path_stack' => [ __DIR__ . '/../view', ], 'template_map' => [ 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', ], ],
.phtml
view filesmodule/Application/view/
If your controller is MainController
and action is homeAction
, Zend will look for:
view/application/main/home.phtml
home.phtml
<h1>Welcome Home (Literal Route)</h1>
view/layout/layout.phtml
.”view/application/main/home.phtml
Wrap inside view/layout/layout.phtml
.'layout/layout' => __DIR__ . '/../view/layout/layout.phtml'
layout.phtml (module\Application\view\layout\layout.phtml)
<?= $this->doctype() ?> <html lang="en"> <head> <meta charset="utf-8"> </head> <body> <div> <?= $this->content ?> </div> </body> </html>
Type |
Description |
---|---|
Literal |
Exact match of a route ( |
Segment |
Dynamic routes with parameters ( |
Regex |
Advanced pattern matching |
Hostname |
Match based on hostname |
Method |
Match based on HTTP method (GET, POST) |