Skip to content
Advertisement

Change Routing in Zend Framework for SEO

I am new to Zend framework and using framework 2.0 , And I want user Friendly URL for My Site. Right Now my URL for Contact us page is http://local.example.com/application/index/contact but I want http://local.example.com/contact like this.
My Module name is Application and Controller name is IndexController (moduleApplicationsrcApplicationControllerIndexController.php) Here is my module.config file

namespace Application;

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'ZendMvcRouterHttpLiteral',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'ApplicationControllerIndex',
                        'action'     => 'index',
                    ),
                ),
            ),
            // The following is a route to simplify getting started creating
            // new controllers and actions without needing to create a new
            // module. Simply drop new controllers in, and you can access them
            // using the path /application/:controller/:action
            'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/application',
                    'defaults' => array(
                        '__NAMESPACE__' => 'ApplicationController',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action][/:id]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
            'reset' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/reset[/:action]',
                    'constraints' => array(
                        'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller'    => 'ApplicationControllerReset',
                        'action'        => 'change-password',
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'abstract_factories' => array(
            'ZendCacheServiceStorageCacheAbstractServiceFactory',
            'ZendLogLoggerAbstractServiceFactory',
        ),
        'factories' => array(
            'translator' => 'ZendMvcServiceTranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'ApplicationControllerIndex' => 'ApplicationControllerIndexController',
            'ApplicationControllerReset' => 'ApplicationControllerResetController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'layout/pagination'           => __DIR__ . '/../view/layout/pagination.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'flash/message'           => __DIR__ . '/../view/layout/FlashMessage.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
        'strategies' => array(
            'ViewJsonStrategy',
        ),
    ),
    // Placeholder for console routes
    'console' => array(
        'router' => array(
            'routes' => array(
            ),
        ),
    ),
);

Advertisement

Answer

You need to add a new Literal route in your configuration, like this:

return array(
    'router' => array(
        'routes' => array(
            // ...
            // Put this AFTER "reset" route
            'contact' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/contact',
                    'defaults' => array(
                        '__NAMESPACE__' => 'ApplicationController',
                        'controller'    => 'Index',
                        // UPDATE THIS FIELD WITH THE ACTUAL ACTION'S NAME
                        'action'        => 'contact', 
                    ),
                )
            ),
        ),
        // ..
    ),
);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement