2012-12-30 195 views
3

我剛剛構建了一個zf2項目,並且出現配置問題。ZF2路由配置

當我去mydomain.com這個路由如配置文件中指定的應用程序模塊,索引控制器,索引操作。

但是,如果我鍵入mydomain.com/otheraction,這不會路由到應用程序模塊,索引控制器,其他操作。當然,因爲它沒有配置這樣做。

所以我的問題是如何配置應用程序來做到這一點?我已經添加了我的Application/config/module.config.php文件和主配置文件。謝謝!

module.config.php

<?php 
/** 
* Zend Framework (http://framework.zend.com/) 
* 
* @link  http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository 
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) 
* @license http://framework.zend.com/license/new-bsd New BSD License 
*/ 

return array(
    'router' => array(
     'routes' => array(
      'home' => array(
       'type' => 'Zend\Mvc\Router\Http\Literal', 
       'options' => array(
        'route' => '/', 
        'defaults' => array(
         'controller' => 'Application\Controller\Index', 
         '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__' => 'Application\Controller', 
         'controller' => 'Index', 
         'action'  => 'index', 
        ), 
       ), 
       'may_terminate' => true, 
       'child_routes' => array(
        'default' => array(
         'type' => 'Segment', 
         'options' => array(
          'route' => '/[:controller[/:action]]', 
          'constraints' => array(
           'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
           'action'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
          ), 
          'defaults' => array(
          ), 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
    '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', 
     ), 
    ), 
); 

application.config.php

<?php 
return array(
    'modules' => array(
     'Application', 
     'ZfcBase', 
     'ZfcUser', 
    ), 
    'module_listener_options' => array(
     'config_glob_paths' => array(
      'config/autoload/{,*.}{global,local}.php', 
     ), 
     'module_paths' => array(
      './module', 
      './vendor', 
     ), 
    ), 
); 

回答

2

配置的默認路由是接受這種格式的任何URL,這是標準的格式

mydomain.com/ 
mydomain.com/applicatiom/controller/action (standard format) 

你的情況mydomain.com/otheraction路由器期待模塊otheraction,但它不存在。

,如果你需要爲上述指定路徑下

'otheraction' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal', 
    'options' => array(
     'route' => '/otheraction', 
     'defaults' => array(
      'controller' => 'Application\Controller\Index', 
      'action'  => 'otheraction', 
     ), 
    ), 
), 
+0

感謝@Raj有一個條目有一個路線!我試過了,但當我訪問mydomain.com/application/index/someaction頁面加載,但我的圖像搜索mydomain.com/application/index/images而不是mydomain.com/images。任何想法爲什麼? – maephisto

+0

引用相對於你的根文件夾ex的圖像。圖片/ yourimage.jpg – Raj

2

在路由子陣,添加類似於 '家' 條目下一個條目,例如:

'user' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal', 
    'options' => array(
     'route' => '/user', 
     'defaults' => array(
      'controller' => 'Application\Controller\User', 
      'action'  => 'some-action', 
     ), 
    ), 
), 

這會將您的網頁/用戶路由到您的UserController中的someActionAction