1

我开始了 ZF2 的冒险。我进入了我的应用程序默认模块应用程序。在我的项目中,我添加了一个名为 User 的新模块。在这个模块中,我得到了登录控制器和注册表控制器。现在是添加 ACL 的时候了。我从本教程http://ivangospodinow.com/zend-framework-2-acl-setup-in-5-minutes-tutorial/中做到这一点。

但是我的 module.acl.php 文件有问题:

<?php
return array(
        'guest'=> array(
                'home',
                'login',
                'register'
        ),
        'admin'=> array(
                'admin',
                'delete-user'
        ),
);

现在,当我输入地址时:/users/register 显示消息:

Fatal error: Uncaught exception 'Zend\Permissions\Acl\Exception\InvalidArgumentException' with message 'Resource 'users/default' not found'

为什么这告诉我找不到“用户/默认”?我无法理解。

如果有必要,我可以从文件中添加代码。

4

2 回答 2

2

您没有定义默认控制器和操作,这就是您收到错误的原因。
您的 ACL 是基于路由名称的,所以这里是一个示例,它应该如何工作:

        'login' => array( // This is name of route
            'type' => 'literalt',
            'options' => array(
                'route' => '/users/login',
                'defaults' => array(
                    'controller' => 'Users\Controller\Login',
                    'action' => 'index',
                ),
            ),

        'register' => array( // This is name of route
            'type' => 'segment',
            'options' => array(
                'route' => '/users/register[/:action]',
                'constraints' => array(
                    'action'     => '[a-zA-Z][a-zA-Z0-9_-]+',
                ),
                'defaults' => array(
                    'controller' => 'Users\Controller\Register',
                    'action' => 'index',
                ),
            ),

如您所见,用户可以访问所有控制器,如果您需要更多动态和高级 ACL,您可以尝试:

'controllers' => array(
    'invokables' => array(
        'login' => 'Users\Controller\LoginController',
        'register' => 'Users\Controller\RegisterController',
    ),
),
    'users' => array(
        'type' => 'segment',
        'options' => array(
            'route' => '/users[/:controller][/:action]',
            'constraints' => array(
                'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
                'action'     => '[a-zA-Z][a-zA-Z0-9_-]+',
            ),
            'defaults' => array(
                'controller' => 'Users\Controller\Default',
                'action' => 'index',
            ),
        ),

比您的 checkAcl() 更改:

$route = $e -> getRouteMatch() -> getMatchedRouteName();

$params = $e->getRouteMatch()->getParams();
$route = $params['controller']."\".$params['action'];

这就是您定义角色和资源的方式:

return array(
        'guest'=> array(
                'login\login',
                'register\index'
                'register\checkLogin'
        ),
        'admin'=> array(
                'admin\statistics',
                'admin\users'
        ),
);
于 2013-11-14T21:23:05.270 回答
0
<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Users\Controller\Index' =>
            'Users\Controller\IndexController',
            'Users\Controller\Register' =>
            'Users\Controller\RegisterController',
                'Users\Controller\Login' => 'Users\Controller\LoginController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'users' => array(
                'type' => 'Literal',
                'options' => array(
                    // Change this to something specific toyour module
                    'route' => '/users',
                    'defaults' => array(
                        // Change this value to reflect the namespace in which
                        // the controllers for your module arefound
                        '__NAMESPACE__' => 'Users\Controller',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    // This route is a sane default whendeveloping a module;
                    // as you solidify the routes for your module, however,
                    // you may want to remove it and replace it with more
                    // specific routes.
                    '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(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'users' => __DIR__ . '/../view',
        ),
    ),
);

这来自用户模块。如果您愿意,我可以从应用程序模块配置中显示。我也有模块。这来自应用程序模块:

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 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(
        'abstract_factories' => array(
            'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
            'Zend\Log\LoggerAbstractServiceFactory',
        ),
        'aliases' => array(
            'translator' => 'MvcTranslator',
        ),
    ),
    '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',
        ),
    ),
    // Placeholder for console routes
    'console' => array(
        'router' => array(
            'routes' => array(
            ),
        ),
    ),
);
于 2013-11-14T19:40:30.807 回答