Zendframework how to create route

Last Updated: January 26, 2018

I am going to create routes based on the information of the below table

URL Action Controller
http://localhost:8000/products index ProductController
http://localhost:8000/products/create create ProductController
http://localhost:8000/products/update/1 update ProductController
http://localhost:8000/products/delete/1 delete ProductController

You can add the route entry in module/Application/config/module.config.ph file
I will expain two methods to create routes

Method 1

You can create four seperate routes by giving them diffrent names

return [
    'router' => [
        'routes' => [
              // other codes
            'product' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/products',
                    'defaults' => [
                        'controller' => Controller\ProductController::class,
                        'action'     => 'index',
                    ],
                ],
            ],

            'product.create' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/products/create',
                    'defaults' => [
                        'controller' => Controller\ProductController::class,
                        'action'     => 'create',
                    ],
                ],
            ],

            'product.update' => [
                'type' => Segment::class,
                'options' => [
                    'route'    => '/products/update/:id',
                    'defaults' => [
                        'controller' => Controller\ProductController::class,
                        'action'     => 'update',
                    ],
                ],
            ],

            'product.delete' => [
                'type' => Segment::class,
                'options' => [
                    'route'    => '/products/delete/:id',
                    'defaults' => [
                        'controller' => Controller\ProductController::class,
                        'action'     => 'delete',
                    ],
                ],
            ],

             // other codes
         ]
    ]
]

Method 2

In this method you can use the child routes

return [
    'router' => [
        'routes' => [
          // other code
          'product' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/products',
                    'defaults' => [
                        'controller' => Controller\ProductController::class,
                        'action'     => 'index',
                    ],
                ],

                'child_routes' => [
                    'create' => [
                        'type' => 'literal',
                        'options' => [
                            'route' => '/create',
                           
                            'defaults' => [
                                'action' => 'create',
                            ],
                        ],
                    ],
                    'update' => [
                        'type' => 'segment',
                        'options' => [
                            'route' => '/update/:id',
                           
                            'defaults' => [
                                'action' => 'update',
                            ],
                        ],
                    ],
                    'delete' => [
                        'type' => 'literal',
                        'options' => [
                            'route' => '/delete/:id',
                           
                            'defaults' => [
                                'action' => 'delete',
                            ],
                        ],
                    ],
                 ]
            ],
// other code
       ],
    ],
];

First you create the route for /products and then you can define the child routes inside it

If you need to get url of a route inside your view file you can the following code

echo $this->url('products');
// gives "/products"
echo $this->url('products/update',array("id"=>2));
// gives  /products/update/2 

echo $this->url('products/create', [], ['force_canonical' => true]); 
// gives http://localhost:8000/products/create