Zend framework2 ZF2:Zend Framework 2.0.3(路由不工作-找不到控制器)

Zend framework2 ZF2:Zend Framework 2.0.3(路由不工作-找不到控制器),zend-framework2,Zend Framework2,我设置了一个演示项目,并得到了两个不同模块之间的路由。然而,在我的实际应用程序中,有应用程序模块和Restful模块,我的Restful模块路由器会启动,但找不到控制器 我像localhost/restful/token/json/1那样访问它 在我的Module.php中,我有一个onDispatch,我正在使用它来转储RouteMatch,我可以看到我的路线实际上正在被拾取 $mvcEvent->getRouteMatch() 当Zend试图加载控制器时,它只是将令牌传递给DispatchL

我设置了一个演示项目,并得到了两个不同模块之间的路由。然而,在我的实际应用程序中,有应用程序模块和Restful模块,我的Restful模块路由器会启动,但找不到控制器

我像
localhost/restful/token/json/1那样访问它

在我的
Module.php
中,我有一个onDispatch,我正在使用它来转储RouteMatch,我可以看到我的路线实际上正在被拾取

$mvcEvent->getRouteMatch()

当Zend试图加载控制器时,它只是将
令牌
传递给
DispatchListener::get
并通过所有父节点,直到它最终抛出一个无法加载控制器的异常。具体地说,最后一个异常会在
DispatchLister::onDispatch
中抛出,并且
$application::ERROR\u CONTROLLER\u NOT\u FOUND

引发的根异常是:

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

#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}
我在
应用程序
模块中进行了相同的转储,它实际上传递了完整的命名空间控制器名称。在我的演示项目中,在额外的模块中,它也在做同样的事情

因此,我不确定我在这个新项目中做错了什么,但一切看起来都和我的演示项目一样。但它总是只是传递给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')
            ),
        ),
    ),
),
);
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());
}

}
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)
{

}
}
Module.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')
            ),
        ),
    ),
),
);
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());
}

}
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)
{

}
}
TokenController.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')
            ),
        ),
    ),
),
);
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());
}

}
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)
{

}
}

您的问题是您在路由中定义了参数
:controller
,它将取代您指定的默认控制器。 将路由定义更改为
'/restful[/token[/:format][/:id]]'

注意:您不能在RESTful API中将数据格式指定为资源uri的一部分,不能将其作为get paramater
?format=xml
传递,也不能在HTTP协议中使用适当的内容协商机制。 zf2中良好的HTTP头实现应该会在这方面有所帮助