Zend Segment Route

🔹 What is a Segment Route?

A Segment route is used when your URL contains dynamic segments (parameters) like /product/42, /user/edit/5, etc.

✅ Use Case:

You want to pass values via the URL and capture them as variables in the controller.


Syntax Pattern

'route' => '/user[/:action[/:id]]'
  • /user – fixed part
  • [:action] – optional action (like view, edit)
  • [:id] – optional parameter (like 5)

Full Example

Step 1: Route Configuration (module/Application/config/module.config.php)

use Application\Controller\MainController;

'router' => [
  'routes' => [
    'user' => [
      'type'  => \Laminas\Router\Http\Segment::class,
      'options' => [
        'route'  => '/user[/:action[/:id]]',
        'defaults' => [
          'controller' => MainController::class,
          'action'   => 'index',
        ],
      ],
    ],
  ],
],

Step 2: Controller Setup

File: module/Application/src/Controller/UserController.php


namespace Application\Controller;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;

class MainController extends AbstractActionController
{
  public function indexAction()
  {
    return new ViewModel(['message' => 'User Index Page']);
  }

  public function viewAction()
  {
    $id = $this->params()->fromRoute('id', 0);
    return new ViewModel(['message' => "Viewing user with ID: $id"]);
  }

  public function editAction()
  {
    $id = $this->params()->fromRoute('id', 0);
    return new ViewModel(['message' => "Editing user with ID: $id"]);
  }
}

Step 3: View Files

view/application/main/index.phtml

<h2><?= $this->message ?></h2>


view/application/main/view.phtml

<h2><?= $this->message ?></h2>


view/application/main/edit.phtml

<h2><?= $this->message ?></h2>

Access in Browser

URL

Action Triggered

View

/user

indexAction()

index.phtml

/user/view/5

viewAction() with id = 5

view.phtml

/user/edit/12

editAction() with id = 12

edit.phtml


Full Example Dynamic Route

Step 1: Route Configuration (module/Application/config/module.config.php)


use Application\Controller\MainController;

'blog' => [
    'type' => \Laminas\Router\Http\Segment::class,
    'options' => [
        'route' => '/blog[/:category][/:slug]',
        'defaults' => [
            'controller' => MainController::class,
            'action' => 'hello',
        ],
        'constraints' => [
            'category' => '[a-zA-Z0-9_-]+',
            'slug' => '[a-zA-Z0-9_-]+',
        ],
    ],
],



Step 2: Controller Setup (module\Application\src\Controller\MainController.php)


<?php
namespace Application\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;

class MainController extends AbstractActionController
{
    public function helloAction()
    {
        $category = $this->params()->fromRoute('category', 'Module');
        $slug = $this->params()->fromRoute('slug', 'Guest');

        return new ViewModel([
            'category' => $category,
            'slug' => $slug,
        ]);
    }
}

Step 3: View Files (view/application/main/hello.phtml)

<h1>Category, <?= $this->escapeHtml($category) ?>!</h1>
<h1>Slug, <?= $this->escapeHtml($slug) ?>!</h1>

Access in Browser

URL

Matched Action

Output

/blog

helloAction()

Category, Module!

Slug, Guest!

/archive/tech

helloAction()

Category, tech!

Slug, Guest!

/archive/tech/tech_slug

helloAction()

Category, tech!

Slug, tech_slug!


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.