【问题标题】:Zend Framework 2 add AclZend Framework 2 添加 Acl
【发布时间】:2013-11-27 22:10:44
【问题描述】:

我从 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'

为什么这告诉我找不到“用户/默认”?看不懂。

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

【问题讨论】:

  • 你能显示module.config.php吗?

标签: zend-framework2 acl


【解决方案1】:

您没有定义默认控制器和操作,这就是您得到错误的原因。
您的 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'
        ),
);

【讨论】:

  • 应将$route = $params['controller']."\".$params['action']; 更改为$route = $params['controller']."\\".$params['action']; 以避免语法错误。
【解决方案2】:
<?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(
            ),
        ),
    ),
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多