2012-11-16 92 views
1

我設置了一個演示項目,並讓路由在兩個不同模塊之間工作。然而,在我的真實應用程序中,擁有應用程序模塊和Restful模塊後,我寧靜的模塊路由器啓動,但無法找到控制器。ZF2:Zend Framework 2.0.3(路由不工作 - 找不到控制器)

我訪問它像localhost/restful/token/json/1

在我Module.php我有我使用轉儲RouteMatch的onDispatch,我可以看到我的路線實際上是被拾起。

$mvcEvent->getRouteMatch()

object(Zend\Mvc\Router\Http\RouteMatch)#185 (3) { 
    ["length":protected]=> 
    int(19) 
    ["params":protected]=> 
    array(2) { 
    ["controller"]=> 
    string(5) "token" 
    ["format"]=> 
    string(4) "json" 
    } 
    ["matchedRouteName":protected]=> 
    string(7) "restful" 
} 

當Zend公司正在努力直到最後拋出一個異常,它無法加載器加載控制器,它只是一個傳遞tokenDispatchListener::get並通過所有的家長。具體來說,最後的異常被拋出與$application::ERROR_CONTROLLER_NOT_FOUND

拋出的根異常是:

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for "token". 

Stack Trace:

#0 /usr/local/zend/apache2/htdocs/EMRAuth/vendor/ZF2/library/Zend/ServiceManager/AbstractPluginManager.php(110): Zend\ServiceManager\ServiceManager->get('token', false) 
#1 /usr/local/zend/apache2/htdocs/EMRAuth/vendor/ZF2/library/Zend/Mvc/Controller/ControllerManager.php(114): Zend\ServiceManager\AbstractPluginManager->get('token', Array, false) 
#2 /usr/local/zend/apache2/htdocs/EMRAuth/vendor/ZF2/library/Zend/Mvc/DispatchListener.php(90): Zend\Mvc\Controller\ControllerManager->get('token') 
#3 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent)) 
#4 /usr/local/zend/apache2/htdocs/EMRAuth/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent)) 
#5 /usr/local/zend/apache2/htdocs/EMRAuth/vendor/ZF2/library/Zend/EventManager/EventManager.php(208): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 
#6 /usr/local/zend/apache2/htdocs/EMRAuth/vendor/ZF2/library/Zend/Mvc/Application.php(297): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 
#7 /usr/local/zend/apache2/htdocs/EMRAuth/public/index.php(22): Zend\Mvc\Application->run() 
#8 {main} 

我做了同樣的轉儲Application模塊中,它實際上是通過全命名空間中的控制器名稱。在我的演示項目中,在額外模塊中它也是這樣做的。

所以我不確定我在做什麼錯在這個新項目,但一切看起來像我的演示項目。但它總是隻傳入DispatchListener「令牌」而不是實際的控制器。

module.config.php

return array(
'controllers' => array(
    'invokables' => array(
     'Restful\Controller\Token' => 'Restful\Controller\TokenController' 
    ), 
), 
'router' => array(
    'routes' => array(
     'restful' => array(
      'type' => 'Zend\Mvc\Router\Http\Segment', 
      'options' => array(
       'route' => '/restful[/:controller[/:format][/:id]]', 
       'constraints' => array(
        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'format' => '(xml|json|sphp)', 
        'id' => '[1-9][0-9]*', 
       ), 
       'defaults' => array(
        '__NAMESPACE__' => 'Restful\Controller', 
        'controller' => 'Restful\Controller\Token', 
        'format' => 'json', 
       ), 
      ), 
     ), 
    ), 
), 
'di' => array(
    'instance' => array(
     'alias' => array(
      'dispatcher' => 'Restful\Response\Dispatcher', 
     ), 
     'Restful\Response\Dispatcher' => array(
      'parameters' => array(
       'options' => include(__DIR__ . '/dispatcher.config.php') 
      ), 
     ), 
    ), 
), 
); 

Module.php

namespace Restful; 

use Zend\EventManager\StaticEventManager; 

class Module 
{ 

protected static $options; 

public function init(\Zend\ModuleManager\ModuleManager $moduleManager) 
{ 
    $events = StaticEventManager::getInstance(); 
    $events->attach('Zend\Mvc\Controller\RestfulController','dispatch', array($this, 'onDispatch'), -100); 
    $events->attach('Zend\Mvc\Application','dispatch.error', array($this, 'onDispatch'),-100); 
} 

public function getConfig() { 
    return include __DIR__ . '/config/module.config.php'; 
} 

public function onDispatch(\Zend\Mvc\MvcEvent $mvcEvent) 
{ 
    $result = $mvcEvent->getResult(); 
    var_dump($mvcEvent->getRouteMatch()); 
    if ($mvcEvent->isError()) 
    { 
     $vars = $result->getVariables(); 
     $errorId = $vars->offsetGet('reason'); 
     $errorMessage=$vars->offsetGet('message'); 
     $content = array(
      'error' => (is_null($errorId)) ? 'notfound-error' : $errorId, 
      'message' => $errorMessage, 
     ); 

    } else { 
     $content = array(
      'content' => (is_array($result)) ? $result : $result->getVariables(), 
     ); 
    } 

    $match = $mvcEvent->getRouteMatch(); 
    $format = (empty($match)) ? 'json' : strtolower($mvcEvent->getRouteMatch()->getParam('format')); 

    $dispatcher = $mvcEvent->getTarget()->getServiceManager()->get('dispatcher'); 

    return $dispatcher->render($format, $content, $mvcEvent->getResponse()); 
} 

} 

TokenController.php

namespace Restful\Controller; 

class TokenController extends \Zend\Mvc\Controller\AbstractRestfulController 
{ 
public function getList() 
{ 
    return array(
     1 => array(
      'id' => 1, 
      'title' => 'Title #1', 
     ), 
     2 => array(
      'id' => 2, 
      'title' => 'Title #2', 
     ), 
    ); 
} 

public function get($id) 
{ 
    return array(
     'id' => $id, 
     'title' => 'Title #1', 
    ); 
} 

public function create($data) 
{ 

} 

public function update($id, $data) 
{ 

} 

public function delete($id) 
{ 

} 
} 

回答

0

你的問題是,你在你的路線定義的參數:controller,指定其取代默認的控制器。 你的路由定義更改爲'/restful[/token[/:format][/:id]]'

由於不相關的注意事項:你不能指定的數據格式,如REST風格的API資源的URI的一部分,把它作爲獲得paramater ?format=xml或HTTP協議使用正確的內容協商機制。 zf2中良好的HTTP頭實現應該可以幫助你。