2

Is there a way to access a url that is different to the controller name?

Say, for my real life example, that I want to access this url http://www.example.com/men with men being a category. My controller would be CategoryController and it would display information from a database that would be appropriate to whatever category had been selected.

The reason I need it like this is because I need to support multiple sites so I can't have a controller for each category. Is there a way to do this? I'm fairly new to the 2 Framework but have decent knowledge of 1.12. Thanks.

1 Answer 1

1

If I undestand you correctly, you may resolve your task by routing configuration.

Module's module.config.php file:

return array(
    'router' => array(
        'routes' => array(
            '<ROUTE_NAME>' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '/:category',
                    'constraints' => array(
                        'category' => '<CATEGORY_PATTERN>', // usually [a-zA-Z0-9-_]+
                    ),
                    'defaults' => array(
                        '__NAMESPACE__' => '<CategoryController Namespace>',
                        'controller' => 'Category',
                        'action' => '<Controller method without "action" postfix>',
                    ),
                ),
            ),
        ),
    ),
);

Inside controller category accessed by:

$category = $this->params()->fromRoute('category');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.