这是我的目录结构
-> = folder
- = file
wamp/www/zf2crud
->module
->Application
->CsnUser
->config
-module.config.php
->language
- ..
->src
->CsnUser
->Controller
-UserController.php
->view
->csn-user
->user
-create.phtml
-delete.phtml
-index.phtml
-update.phtml
->error
..
->layout
..
-Module.php
我按照本教程开发了一个模块CsnUser。 reference。旧版本。所以我从我的默认文件夹Application文件夹中复制了module.config.php。之后,如果我输入http://localhost/zf2crud/public/我的zend框架deafault主页工作正常并输入http://localhost/zf2crud/public/csn-user它继续我的CsnUser索引页面它也没关系。但如果我输入http://localhost/zf2crud/public/csn-user/user/update/某事给出404错误,我认为问题是我的module.config.php
我需要继续我的另一个视图,如更新删除创建页面如何执行此操作。
这是我的CsnUser配置文件。 module.config.php
namespace CsnUser;
return array(
'router' => array(
'routes' => array(
'csn_user' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/csn-user',
'defaults' => array(
'controller' => 'CsnUser\Controller\user',
'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
'csnuser' => array(
'type' => 'Literal',
'options' => array(
'route' => '/csnuser',
'defaults' => array(
'__NAMESPACE__' => 'CsnUser\Controller',
'controller' => 'user',
'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(
// 'CsnUser\Controller\User' => Controller\UserController::class
'CsnUser\Controller\user' => 'CsnUser\Controller\UserController'
),
),
'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',
'csn-user/user/index' => __DIR__ . '/../view/csn-user/user/index.phtml',
'csn-user/user/update' => __DIR__ . '/../view/csn-user/user/update.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(
),
),
),
);
答案 0 :(得分:0)
1)您应该尝试直接从公用文件夹提供应用程序。您现在http://localhost/zf2crud/public/
的位置实际上应该直接提供http://localhost/
。您可以通过配置vhost轻松解决此问题。
2)如果你问我,你的路由器配置似乎有点不对,我会建议这样的话,我想这就是你想要的......
return array(
'router' => array(
'routes' => array(
'csnuser' => array(
'type' => 'Literal',
'options' => array(
'route' => '/csn-user',
'defaults' => array(
'__NAMESPACE__' => 'CsnUser\Controller',
'controller' => 'user',
'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(
'action' => 'index'
)
)
)
)
)
)
)
),
这将匹配如下:
url controller action
------------------------------------------------------------------------------------
http://localhost/csn-user -> CsnUser\Controller\UserController -> index
http://localhost/csn-user/user -> CsnUser\Controller\UserController -> index
http://localhost/csn-user/user/index -> CsnUser\Controller\UserController -> index
http://localhost/csn-user/user/update -> CsnUser\Controller\UserController -> update
http://localhost/csn-user/user/delete -> CsnUser\Controller\UserController -> delete
http://localhost/csn-user/user/create -> CsnUser\Controller\UserController -> create
如果你为公司引入一个新的控制器:
'CsnUser\Controller\company' => 'CsnUser\Controller\CompanyController'
你可以这样匹配
url controller action
------------------------------------------------------------------------------------------
http://localhost/csn-user/company -> CsnUser\Controller\CompanyController -> index
http://localhost/csn-user/company/index -> CsnUser\Controller\CompanyController -> index
http://localhost/csn-user/company/update -> CsnUser\Controller\CompanyController -> update
http://localhost/csn-user/company/delete -> CsnUser\Controller\CompanyController -> delete
http://localhost/csn-user/company/create -> CsnUser\Controller\CompanyController -> create
3)您还可以从路由器中删除整个csn-user
部分。这似乎有点不必要......
return array(
'router' => array(
'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(
'__NAMESPACE__' => 'CsnUser\Controller',
'action' => 'index'
'controller' => 'user'
)
)
)
)
)
),
这将匹配如下:
url controller action
------------------------------------------------------------------------------------
http://localhost -> CsnUser\Controller\UserController -> index
http://localhost/user -> CsnUser\Controller\UserController -> index
http://localhost/user/index -> CsnUser\Controller\UserController -> index
http://localhost/user/update -> CsnUser\Controller\UserController -> update
http://localhost/user/delete -> CsnUser\Controller\UserController -> delete
http://localhost/user/create -> CsnUser\Controller\UserController -> create
如果你为公司引入一个新的控制器:
'CsnUser\Controller\company' => 'CsnUser\Controller\CompanyController'
你可以这样匹配
url controller action
------------------------------------------------------------------------------------------
http://localhost/company -> CsnUser\Controller\CompanyController -> index
http://localhost/company/index -> CsnUser\Controller\CompanyController -> index
http://localhost/company/update -> CsnUser\Controller\CompanyController -> update
http://localhost/company/delete -> CsnUser\Controller\CompanyController -> delete
http://localhost/company/create -> CsnUser\Controller\CompanyController -> create