Zf2 Locale in URL and $this->url

2019-06-05 15:02发布

问题:

I am developing a web app on ZF2 Skeleton App base. I have played with lot of options but failed to get final headway.

I need to route the url as under:

http://myapp/
http://myapp/en/album

to AlbumController/indexAction. Also, links need to work as:

http://myapp/en/album/edit/1
http://myapp/en/album/delete/1

The code generates correct URLs but on clicking returns "404" error

My Application/module.config.php is as under:


return array (
        'router' => array (
                'routes' => array (
                        'home' => array (
                                'type' => 'Zend\Mvc\Router\Http\Literal',
                                'options' => array (
                                        'route' => '/',
                                        'defaults' => array (
                                                'controller' => 'Album\Controller\Album',
                                                'action' => 'index',
                                                'lang'     => 'en',
                                        ) 
                                ) 
                        ),
                        'application' => array (
                                'type' => 'Literal',
                                'options' => array (
                                        'route' => '/application',
                                        'defaults' => array (
                                                '__NAMESPACE__' => 'Application\Controller',
                                                'controller' => 'Index',
                                                'action' => 'index' 
                                        ) 
                                ),
                                'may_terminate' => true,
                                'child_routes' => array (
                                        'default' => array (
                                                'type' => 'Segment',
                                                'options' => array (
                                                        'route' => '[:lang[/album[/:action[/:id]]]]',
                                                        'constraints' => array (
                                                                'lang'     => '[a-z]{2}',
                                                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                                'id' => '[0-9]+'
                                                        ),
                                                        'defaults' => array (
                                                                'controller' => 'Album\Controller\Album',
                                                                'action' => 'index',
                                                                'lang'     => 'en',
                                                        )
                                                ) 
                                        ) 
                                ) 
                        ) 
                ) 
        ),
        'service_manager' => array (
                'factories' => array (
                        'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory' 
                ) 
        ),
        'translator' => array (
                'locale' => 'en_US',
                'translation_file_patterns' => array (
                        array (
                                'type' => 'gettext',
                                'base_dir' => __DIR__ . '/../language',
                                'pattern' => '%s.mo' 
                        ) 
                ) 
        ),
        'controllers' => array (
                'invokables' => array (
                        'Application\Controller\Index' => 'Application\Controller\IndexController' 
                ) 
        ),
        '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',
                        'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
                        'error/404' => __DIR__ . '/../view/error/404.phtml',
                        'error/index' => __DIR__ . '/../view/error/index.phtml' 
                ),
                'template_path_stack' => array (
                        __DIR__ . '/../view' 
                ) 
        ) 
);

My Album/module.config.php has the following router:


'router' => array (
                'routes' => array (
                        'album' => array (
                                'type' => 'segment',
                                'options' => array (
                                        'route' => '[:lang[/album[/:action[/:id]]]]',
                                        'constraints' => array (
                                                'lang'     => '[a-z]{2}',
                                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                'id' => '[0-9]+'
                                        ),
                                        'defaults' => array (
                                                'controller' => 'Album\Controller\Album',
                                                'action' => 'index', 
                                                'lang'     => 'en',
                                        )
                                ),
                        )
                )
        ), 

//////////////////////////////////////////////////////////////////////////////////////// // Now this works fine.

Also, when I do call the $this->url('album',array('action'=>'edit', 'id' => $album->id)); in view file (.phtml), it does'nt return proper url as expected:

http://www.myapp.com/en/edit/id/1

//////////////////////////////////////////////////////////////////////////////////////// //Corrected code works for URL $this->url('album', array('action'=>'edit', 'id' => $album->id)) ///////////////////////////////////////////////////////////////////////////////////////

Appreciate your help in advance.

回答1:

The issue was missing '/' in Album/module.config.php:

'route' => '[:lang[/album[/:action[/:id]]]

should have been:

'route' => '/[:lang[/album[/:action[/:id]]]

Thanks again to all for help.