N
N
Nikolai Gromov2017-05-03 09:44:32
Zend Framework
Nikolai Gromov, 2017-05-03 09:44:32

What about the controller?

Hello! When following a link from the main page to a page from a particular category, I use CategoryController which is a child (by routes) of the Shop module, here is the configuration file where it is described:

'router' => [
        'routes' => [
            'shop' => [
                'type'    => Literal::class,
                'options' => [
                    // Change this to something specific to your module
                    'route'    => '/shop/',
                    'defaults' => [
                        'controller'    => Controller\IndexController::class,
                        'action'        => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [
                    'category' => [
                        'type'    => Segment::class,
                        'options' => [
                            'route'    => 'category/[:action/][:id/]',
                            'constraints' => [
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'id' => '[0-9]+',
                            ],
                            'defaults' => [
                                'controller'    => Controller\CategoryController::class,
                                'action'        => 'index',
                            ]
                        ]
                    ],
                    // You can place additional routes that match under the
                    // route defined above here.
                ],
            ],
        ],
    ],

There is a paginator on the page that is processed by this controller. The page address is obtained
zblog.local/shop/category/1

When you work with the paginator, the address changes to
zblog.local/shop/category/?page=2
, but the id must remain since the controller's work is tied to it. The code that displays the paginator on the page:
<?= $this->paginationControl($products,
            'Sliding',
            'application/partial/paginator', 
            ['route' => 'shop/category']); ?>

As I understand it, in order for the paginator to work correctly in route, you need to add id, but to do this, you need to register a child route for category in the configuration file. I just don't know how to do it correctly, the id must be numbers. Tell me who knows!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
novrm, 2017-06-10
@T_verdisla_V

Means so - write such route.
And about the paginator, I wrote to you further.

'router' => [
    'routes' => [

        /** Configuration of the 'shop' routes. */
        'shop' => [
            'type'    => 'Literal',
            'options' => [
                'route'    => '/shop/',
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [

                /** Configuration of the 'shop/category' routes. */
                'category' => [
                    'type'    => 'Literal',
                    'options' => [
                        'route'    => 'category/',
                        'defaults' => [
                            'controller' => Controller\CategoryController::class,
                            'action'     => 'index',
                        ],
                    ],
                    'may_terminate' => true,
                    'child_routes' => [

                        /** Configuration of the 'shop/category/action' routes. */
                        'action' => [
                            'type' => 'Segment',
                            'options' => [
                                'route' => '[:action]',
                                'constraints' => [
                                    'action' => '(create)',
                                ],
                            ],
                        ],

                        /** Configuration of the 'shop/category/category' routes. */
                        'category' => [
                            'type' => 'Segment',
                            'options' => [
                                'route' => '[:id/]',
                                'constraints' => [
                                    'id' => '[0-9]+',
                                ],
                                'defaults' => [
                                    'id' => null,
                                ],
                            ],
                            'may_terminate' => true,
                            'child_routes' => [

                                /** Configuration of the 'shop/category/category/action' routes. */
                                'action' => [
                                    'type' => 'Segment',
                                    'options' => [
                                        'route' => '[:action]',
                                        'constraints' => [
                                            'action' => '(update|delete)',
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
],

V
Vladimir Zhosan, 2017-05-03
@iaskivsky

Am I the only one who sees the routes in this framework as miserable?

O
OrlandoST, 2017-05-31
@OrlandoST

Pass id in parameters.
Here is an example:
<?= $this->paginationControl($products,
'Sliding',
'application/partial/paginator',
['route' => 'shop/category', 'params' => array('id' = >1')]); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question