我遇到了一个大问题,因为我可以通过IndexController看到index.html给它,但是如果我创建了另一个动作,例如getdataAction()我有下一个错误:
我认为我没有配置任何东西,但这些是我的文件:
module_config.php
namespace Application;
return array(
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/Entity')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'application_entities'
)
)
)
),
'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(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'factories' => array(
'translator' => 'Zend\Mvc\Service\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' => Controller\IndexController::class
//'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',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
IndexController.php
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Entity\UserEntity;
class IndexController extends AbstractActionController
{
protected $_objectManager;
public function indexAction(){
try{
$users = $this->getObjectManager()->getRepository('\Application\Entity\UserEntity')->findAll();
foreach ($users as $user){
echo "[id]: " . $user->getId() . " Nombre: " . $user->getFullName() . "<br />";
}
}catch(Exception $ex){
echo "error: " . $ex->getMessage();
}
return new ViewModel(array("users" => $users));
}
public function getdataAction(){
return new ViewModel();
}
protected function getObjectManager()
{
if (!$this->_objectManager) {
$this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
}
return $this->_objectManager;
}
}
目录结构是下一个:
答案 0 :(得分:2)
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/Application',
'defaults' => array(
__NAMESPACE__ => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
此代码错误,您必须引用此 NAMESPACE 键
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/Application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
将解决此问题
索引(解析为无效的控制器类或别名:索引)
另一方面,你使用它:
'invokables' => array(
'Application\Controller\Index' => Controller\IndexController::class
您几乎拥有此权利,但您必须填写完整的合格类名称(FQCN)
'invokables' => array(
'Application\Controller\Index' => Application\Controller\IndexController::class
编辑:
您将主要路线定义为 /应用和
'defaults' => array(
__NAMESPACE__ => 'Application\Controller',
'controller' => 'Index',
ucfirst
这里很重要。试试吧。
http://localhost/Application/Index/getdata
和
http://localhost/application/index/getdata
答案 1 :(得分:0)
您没有任何行动途径。
骨架应用程序将所有路由定义为标准,您可以通过以下方式调用:
/application/:controller/:action
所以你应该可以使用:
/application/index/getdata
要获得更好的路线,您需要在module_config中定义一个。