Zend Routing

What is Routing?

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:

  • Controller: UserController
  • Action: profileAction()

Where is Routing Defined? (module/Application/config/module.config.php)

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',
        ],
      ],
    ],
  ],
],


  • Defines a route named home
  • Matches exactly / (homepage)
  • When the homepage is opened, it will run:


MainController::homeAction()

'controllers' – Registering Controllers (module/Application/config/module.config.php)

'controllers' => [
  'factories' => [
    Controller\MainController::class => InvokableFactory::class,
  ],
],


  • Tells the framework how to create MainController
  • Uses InvokableFactory since the controller has no constructor parameters
  • Required to make the controller load properly


MainController.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();
    }
}


  • Each public method ending with 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',
    ],
],


template_path_stack

  • Tells Zend where to find .phtml view files
  • In this case, it will look in:
module/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>


template_map

  • “Wrap all rendered views inside view/layout/layout.phtml.”
  • Here 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>

 Route Types in Zend

Type

Description

Literal

Exact match of a route (/about)

Segment

Dynamic routes with parameters (/user/:id)

Regex

Advanced pattern matching

Hostname

Match based on hostname

Method

Match based on HTTP method (GET, POST)

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.