### Laminas Router Regex Route Factory Example Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Demonstrates the creation of a Regex route using the factory method. This route uses a regular expression with named captures to match a blog post URL and includes default values and a specification for URL assembly. ```php $route = Regex::factory([ 'regex' => '/blog/(?[a-zA-Z0-9_-]+)(\.(?(json|html|xml|rss)))?', 'defaults' => [ 'controller' => 'Application\Controller\BlogController', 'action' => 'view', 'format' => 'html', ], 'spec' => '/blog/%id%.%format%', ]); ``` -------------------------------- ### Laminas Router Scheme Route Factory Example Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Shows how to create a Scheme route using its factory method. This route matches the URI scheme, in this case 'https', and sets a default value upon successful matching. ```php $route = Scheme::factory([ 'scheme' => 'https', 'defaults' => [ 'https' => true, ], ]); ``` -------------------------------- ### Install laminas-mvc-console Package Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/migration/v2-to-v3.md This command installs the laminas-mvc-console package using Composer. This is necessary if your application relies on console routing, which has been removed from laminas-router v3 and moved to this separate package. ```bash $ composer require laminas/laminas-mvc-console ``` -------------------------------- ### Install laminas-router with Composer Source: https://github.com/laminas/laminas-router/blob/3.15.x/README.md This command installs the laminas-router library using Composer, the dependency manager for PHP. It fetches the package and its dependencies, making them available for your project. ```bash composer require laminas/laminas-router ``` -------------------------------- ### Laminas Router Placeholder Route Configuration Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Example configuration for a Placeholder route in Laminas Router, used for reusable modules. It demonstrates how a module can provide routes anchored by a placeholder that can be replaced by the consuming application. ```php return [ 'auth' => [ 'type' => \Laminas\Mvc\Router\Http\Placeholder::class, 'child_routes' => [ 'login' => [ 'type' => \Laminas\Mvc\Router\Http\Literal::class, 'options' => [ 'route' => '/login', 'defaults' => [ 'controller' => AuthController::class, 'action' => 'login' ], ], ], 'register' => [ 'type' => \Laminas\Mvc\Router\Http\Literal::class, 'options' => [ 'route' => '/register', 'defaults' => [ 'controller' => RegistrationController::class, 'action' => 'register' ], ], ], ], ], ]; ``` ```php return [ 'auth' => [ 'type' => \Laminas\Mvc\Router\Http\Literal::class, 'options' => [ 'route' => '/auth', ], ], ]; ``` -------------------------------- ### HTTP Routing Configuration with Literal Routes (PHP) Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Provides an example of configuring HTTP routes within a Laminas module configuration file. It shows how to define two literal routes, 'home' and 'contact', each with a specific URI path and associated default controller and action. This structure is common for setting up basic application routes. ```php return [ 'router' => [ 'routes' => [ // Literal route named "home" 'home' => [ 'type' => 'literal', 'options' => [ 'route' => '/', 'defaults' => [ 'controller' => 'Application\Controller\IndexController', 'action' => 'index', ], ], ], // Literal route named "contact" 'contact' => [ 'type' => 'literal', 'options' => [ 'route' => 'contact', 'defaults' => [ 'controller' => 'Application\Controller\ContactController', 'action' => 'form', ], ], ], ], ], ]; ``` -------------------------------- ### Configure Laminas Router RoutePluginManager Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Illustrates how to set up a `LaminasRouterRoutePluginManager` by defining and registering various HTTP route types. This manager is essential for the `Part` route and other route types to correctly resolve and instantiate their corresponding classes. The example shows setting invokable classes for common route types. ```php $routePlugins = new Laminas\Router\RoutePluginManager(); $plugins = [ 'hostname' => 'Laminas\Router\Http\Hostname', 'literal' => 'Laminas\Router\Http\Literal', 'part' => 'Laminas\Router\Http\Part', 'regex' => 'Laminas\Router\Http\Regex', 'scheme' => 'Laminas\Router\Http\Scheme', 'segment' => 'Laminas\Router\Http\Segment', 'wildcard' => 'Laminas\Router\Http\Wildcard', 'method' => 'Laminas\Router\Http\Method', ]; foreach ($plugins as $name => $class) { $routePlugins->setInvokableClass($name, $class); } ``` -------------------------------- ### Adding Routes to Router Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Shows two methods for adding routes to a router instance: individually using addRoute() with pre-instantiated route objects, and in bulk using addRoutes() with route configurations for lazy-loading. ```php // One at a time: $route = Literal::factory([ 'route' => '/foo', 'defaults' => [ 'controller' => 'foo-index', 'action' => 'index', ], ]); $router->addRoute('foo', $route); // In bulk: $router->addRoutes([ // using already instantiated routes: 'foo' => $route, // providing configuration to allow lazy-loading routes: 'bar' => [ 'type' => 'literal', 'options' => [ 'route' => '/bar', 'defaults' => [ 'controller' => 'bar-index', 'action' => 'index', ], ], ], ]); ``` -------------------------------- ### Hostname Route Configuration with Constraints and Defaults Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Configures a Hostname route to match specific subdomains with constraints and provides default values for route matches. It uses the Hostname::factory method for creation. ```php $route = Hostname::factory([ 'route' => ':subdomain.domain.tld', 'constraints' => [ 'subdomain' => 'fw\\d{2}', ], 'defaults' => [ 'type' => 'json', ], ]); ``` -------------------------------- ### Literal Route Configuration Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Sets up a Literal route for exact URI path matching. It specifies the path to match and the default parameters to return upon a successful match. ```php $route = Literal::factory([ 'route' => '/foo', 'defaults' => [ 'controller' => 'Application\\Controller\\IndexController', 'action' => 'foo', ], ]); ``` -------------------------------- ### Segment Route Factory Configuration (PHP) Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Demonstrates how to create a Segment route using its factory method. This includes defining the route pattern, constraints for segments, and default values for parameters. The 'route' defines the URI structure, 'constraints' use regex for validation, and 'defaults' provide fallback values. ```php $route = Segment::factory([ 'route' => '/:controller[/:action]', 'constraints' => [ 'controller' => '[a-zA-Z][a-zA-Z0-9_-]+', 'action' => '[a-zA-Z][a-zA-Z0-9_-]+', ], 'defaults' => [ 'controller' => 'Application\Controller\IndexController', 'action' => 'index', ], ]); ``` -------------------------------- ### Generating URLs with Laminas Url Plugin Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Demonstrates how to use the `url` view helper or controller plugin in Laminas to generate URLs based on previously matched route parameters. This allows for dynamic URL creation that respects the configured routes. ```php // reuse the route matched parameters to generate URLs echo $this->url('modules.laminas.dev/index', [], [], true); echo $this->url('packages.laminas.dev/index', [], [], true); ``` -------------------------------- ### Hostname Route Configuration with Constraints Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Defines a Hostname route that matches a specific subdomain pattern using regular expression constraints. The route returns the matched subdomain as a parameter. ```php $route = Hostname::factory([ 'route' => ':subdomain.domain.tld', 'constraints' => [ 'subdomain' => 'fw\\d{2}', ], ]); ``` -------------------------------- ### Method Route Configuration for Multiple HTTP Methods Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Configures a Method route to match requests using either POST or PUT HTTP methods. It also defines default parameters for the route match. ```php $route = Method::factory([ 'verb' => 'post,put', 'defaults' => [ 'controller' => 'Application\\Controller\\IndexController', 'action' => 'form-submit', ], ]); ``` -------------------------------- ### Create Laminas Router Http Part with Nested Routes Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Demonstrates the usage of the `Part::factory` method to construct a hierarchical routing structure. This allows defining multiple nested routes with different types (e.g., literal) and default parameters, enabling complex URI path matching. Requires a `RoutePluginManager` instance. ```php $route = Part::factory([ 'route' => [ 'type' => 'literal', 'options' => [ 'route' => '/', 'defaults' => [ 'controller' => 'Application\Controller\IndexController', 'action' => 'index', ], ], ], 'route_plugins' => $routePlugins, 'may_terminate' => true, 'child_routes' => [ 'blog' => [ 'type' => 'literal', 'options' => [ 'route' => '/blog', 'defaults' => [ 'controller' => 'Application\Controller\BlogController', 'action' => 'index', ], ], 'may_terminate' => true, 'child_routes' => [ 'rss' => [ 'type' => 'literal', 'options' => [ 'route' => '/rss', 'defaults' => [ 'action' => 'rss', ] ], 'may_terminate' => true, 'child_routes' => [ 'subrss' => [ 'type' => 'literal', 'options' => [ 'route' => '/sub', 'defaults' => [ 'action' => 'subrss', ], ], ], ], ], ], ], 'forum' => [ 'type' => 'literal', 'options' => [ 'route' => 'forum', 'defaults' => [ 'controller' => 'Application\Controller\ForumController', 'action' => 'index', ], ], ], ], ]); ``` -------------------------------- ### Generating URLs for Nested Routes in Laminas (PHP) Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Demonstrates how to generate URLs for parent and child routes defined in Laminas Router using the url() helper. It shows generating URLs for the 'blog', 'blog/post', and 'blog/rss' routes, including passing parameters for dynamic segments. ```php echo $this->url('blog'); // gives "/blog" echo $this->url('blog/post', ['slug' => 'my-post']); // gives "/blog/my-post" echo $this->url('blog/rss'); // gives "/blog/rss" ``` -------------------------------- ### Laminas\Router\Http\Method Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md The Method route matches the HTTP method (verb) of the request, supporting single or multiple methods. ```APIDOC ## Laminas\Router\Http\Method ### Description Matches the HTTP method (verb) of the request. Can be configured to match against a single method or a comma-separated list of methods. ### Method Factory method for creating a Method route. ### Endpoint N/A (Route configuration) ### Parameters #### Request Body - **verb** (string) - Required - The HTTP method(s) to match (e.g., 'post' or 'post,put'). - **defaults** (array) - Optional - An associative array of default parameters to return on a match. ### Request Example ```php $route = Method::factory([ 'verb' => 'post,put', 'defaults' => [ 'controller' => 'Application\Controller\IndexController', 'action' => 'form-submit', ], ]); ``` ### Response #### Success Response (Match) - **matched_parameters** (array) - An associative array containing parameters defined in 'defaults' if the HTTP method matches. #### Response Example If the request HTTP method is 'POST' or 'PUT', the matched parameters would be: ```json { "controller": "Application\\Controller\\IndexController", "action": "form-submit" } ``` ``` -------------------------------- ### Laminas Router Configuration with Child Routes (PHP) Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Defines a routing configuration for Laminas Router, featuring a parent route 'blog' with nested 'post' (segment) and 'rss' (literal) child routes. This structure allows for organized and hierarchical URL definitions. ```php return [ 'router' => [ 'routes' => [ // Literal route named "home" 'home' => [ 'type' => 'literal', 'options' => [ 'route' => '/', 'defaults' => [ 'controller' => 'Application\Controller\IndexController', 'action' => 'index', ], ], ], // Literal route named "blog", with child routes 'blog' => [ 'type' => 'literal', 'options' => [ 'route' => '/blog', 'defaults' => [ 'controller' => 'Application\Controller\BlogController', 'action' => 'index', ], ], 'may_terminate' => true, 'child_routes' => [ // Segment route for viewing one blog post 'post' => [ 'type' => 'segment', 'options' => [ 'route' => '/[:slug]', 'constraints' => [ 'slug' => '[a-zA-Z0-9_-]+', ], 'defaults' => [ 'action' => 'view', ], ], ], // Literal route for viewing blog RSS feed 'rss' => [ 'type' => 'literal', 'options' => [ 'route' => '/rss', 'defaults' => [ 'action' => 'rss', ], ], ], ], ], ], ], ]; ``` -------------------------------- ### Laminas Router Hostname Configuration Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Defines routes using LaminasRouterHttpHostname to match various domain patterns, including optional subdomains. It specifies constraints for domain parts and sets up child routes for literal path matches. This configuration allows for flexible routing based on hostnames. ```php return [ 'router' => [ 'routes' => [ 'modules.laminas.dev' => [ 'type' => 'Laminas\Router\Http\Hostname', 'options' => [ 'route' => ':4th.[:3rd.]:2nd.:1st', // domain levels from right to left 'constraints' => [ '4th' => 'modules', '3rd' => '.*?', // optional 3rd level domain such as .ci, .dev or .test '2nd' => 'laminas', '1st' => 'com', ], // Purposely omit default controller and action // to let the child routes control the route match ], // child route controllers may span multiple modules as desired 'child_routes' => [ 'index' => [ 'type' => 'Laminas\Router\Http\Literal', 'options' => [ 'route' => '/', 'defaults' => [ 'controller' => 'Module\Controller\Index', 'action' => 'index', ], ], 'may_terminate' => true, ], ], ], 'packages.laminas.dev' => [ 'type' => 'Laminas\Router\Http\Hostname', 'options' => [ 'route' => ':4th.[:3rd.]:2nd.:1st', // domain levels from right to left 'constraints' => [ '4th' => 'packages', '3rd' => '.*?', // optional 3rd level domain such as .ci, .dev or .test '2nd' => 'laminas', '1st' => 'com', ], // Purposely omit default controller and action // to let the child routes control the route match ], // child route controllers may span multiple modules as desired 'child_routes' => [ 'index' => [ 'type' => 'Laminas\Router\Http\Literal', 'options' => [ 'route' => '/', 'defaults' => [ 'controller' => 'Package\Controller\Index', 'action' => 'index', ], ], 'may_terminate' => true, ], ], ], ], ], ]; ``` -------------------------------- ### Extracting Route Parameters Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Demonstrates how to retrieve parameters, such as an 'id', from a RouteMatch object and handle cases where the parameter is missing. ```php $id = $routeMatch->getParam('id', false); if (! $id) { throw new Exception('Required identifier is missing!'); } $entity = $resource->get($id); ``` -------------------------------- ### Laminas\Router\Http\Hostname Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md The Hostname route matches the hostname in the request against specific criteria, supporting wildcards and constraints. ```APIDOC ## Laminas\Router\Http\Hostname ### Description Matches the hostname registered in the request against specific criteria, supporting wildcard segments and constraints for matching. ### Method Factory method for creating a Hostname route. ### Endpoint N/A (Route configuration) ### Parameters #### Request Body - **route** (string) - Required - The hostname pattern to match (e.g., ':subdomain.domain.tld'). - **constraints** (array) - Optional - An associative array of constraints for hostname segments. - **segment_name** (string) - Regex pattern for the segment. - **defaults** (array) - Optional - An associative array of default values for matched segments or additional parameters. ### Request Example ```php $route = Hostname::factory([ 'route' => ':subdomain.domain.tld', 'constraints' => [ 'subdomain' => 'fw\\d{2}', ], 'defaults' => [ 'type' => 'json', ], ]); ``` ### Response #### Success Response (Match) - **matched_parameters** (array) - An associative array containing matched parameters from the hostname segments and defaults. #### Response Example For the example above, if the request hostname is 'fw12.domain.tld', the matched parameters might include: ```json { "subdomain": "fw12", "type": "json" } ``` ``` -------------------------------- ### RouteInterface Definition Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Defines the contract for a route in the Laminas Router. It includes methods for factory creation, matching a request, and assembling URLs from parameters. ```php namespace Laminas\Router; use Laminas\Stdlib\RequestInterface as Request; interface RouteInterface { public static function factory(array $options = []); public function match(Request $request); public function assemble(array $params = [], array $options = []); } ``` -------------------------------- ### RouteStackInterface Definition Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Defines the interface for aggregating and managing multiple routes. It allows adding, removing, and setting routes, and also inherits the basic RouteInterface methods. ```php namespace Laminas\Router; interface RouteStackInterface extends RouteInterface { public function addRoute($name, $route, $priority = null); public function addRoutes(array $routes); public function removeRoute($name); public function setRoutes(array $routes); } ``` -------------------------------- ### Update Namespaces with sed Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/migration/v2-to-v3.md This script recursively finds and replaces the old Laminas MVC Router namespace with the new Laminas Router namespace across files. It assumes a Unix-like environment and modifies files in place. Caution is advised to avoid running this in the project root to prevent unintended changes in vendor directories. ```bash $ for code in $(grep -rl 'Laminas.Mvc.Router' .); do sed --in-place -e 's/Laminas\\Mvc\\Router/Laminas\\Router/g' ${code} done ``` -------------------------------- ### Laminas\Router\Http\Literal Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md The Literal route performs an exact match against the URI path, allowing for simple static route definitions. ```APIDOC ## Laminas\Router\Http\Literal ### Description Performs an exact match of the URI path. Used for defining static routes. ### Method Factory method for creating a Literal route. ### Endpoint N/A (Route configuration) ### Parameters #### Request Body - **route** (string) - Required - The exact path to match (e.g., '/foo'). - **defaults** (array) - Optional - An associative array of default parameters to return on a match. ### Request Example ```php $route = Literal::factory([ 'route' => '/foo', 'defaults' => [ 'controller' => 'Application\Controller\IndexController', 'action' => 'foo', ], ]); ``` ### Response #### Success Response (Match) - **matched_parameters** (array) - An associative array containing parameters defined in 'defaults' if the path matches. #### Response Example If the request path is '/foo', the matched parameters would be: ```json { "controller": "Application\\Controller\\IndexController", "action": "foo" } ``` ``` -------------------------------- ### RouteMatch Class Source: https://github.com/laminas/laminas-router/blob/3.15.x/docs/book/routing.md Represents the result of a successful route match. It holds parameters extracted from the request and the name of the matched route. ```php namespace Laminas\Router; class RouteMatch { public function __construct(array $params); public function setMatchedRouteName($name); public function getMatchedRouteName(); public function setParam($name, $value); public function getParams(); public function getParam($name, $default = null); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.