📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Zend Framework / Laminas Routing in Laminas

Routing in Laminas

6 min read Quiz at the end
Configure Literal and Segment routes in module.config.php with child routes and constraints.

Routing in Laminas

// module/Blog/config/module.config.php
use LaminasRouterHttpSegment;
use LaminasRouterHttpLiteral;

return [
    "router" => [
        "routes" => [
            "blog" => [
                "type"    => Literal::class,
                "options" => [
                    "route"    => "/blog",
                    "defaults" => ["controller" => ControllerPostController::class, "action" => "index"],
                ],
                "may_terminate" => true,
                "child_routes"  => [
                    "post" => [
                        "type"    => Segment::class,
                        "options" => [
                            "route"       => "/[:action[/:id]]",
                            "constraints" => ["action" => "[a-zA-Z][a-zA-Z0-9_-]*", "id" => "[0-9]+"],
                            "defaults"    => ["action" => "index"],
                        ],
                    ],
                ],
            ],
        ],
    ],
];

// Generate URL in view
echo $this->url("blog/post", ["action" => "view", "id" => 42]);
Topic Quiz · 2 questions

Test your understanding before moving on

1. What does Module.php do in Laminas?
💡 Module.php is the entry point for each module providing config and event hooks.
2. What does the route event do?
💡 The route event matches the URL to a route determining controller and action.