codeigniter 路由和重定向

问题描述 投票:0回答:2

当我输入我的基本网址

http://localhost/myproject/admin
时,它会继续将我发送到我的权限页面。
http://localhost/myproject/admin
是base_url()。

我的 core/Controller.php 的工作原理是检查是否可以访问控制器,如果不在忽略列表中,则重定向到其他有权访问页面的权限。

我想知道是否可以添加我的

base_url()
,以便它忽略它并让我可以访问它。我不确定在下面的代码中最好将其添加到哪里。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// I am not using MY_Controller works with Controller fine.
class Controller extends CI_Controller { 

public function __construct() {
    parent::__construct();

    $url = $this->uri->segment(1).'/'.$this->uri->segment(2);

    if (isset($url)) {
        $route = '';

        $segment = explode('/', $url);

        if (isset($segment[0])) {
            $route .= $segment[0];
        }

        if (isset($segment[1])) {
            $route .= '/' . $segment[1];
        }

        // $route would equal example: common/dashboard

        // $segment[0] folder i.e common
        // $segment[1] controller 

        $ignore = array(
            'common/dashboard',
            'common/login',
            'common/forgotten',
            'common/reset',
            'error/not_found',
            'error/permission'
        );

        if (!in_array($route, $ignore)) {
            redirect('permission');
        }
    }
}
}
php codeigniter
2个回答
1
投票

使用 Hook 检查权限:

1 - 创建一个配置文件

config/acl.php
:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$acl = array(
    'role_permission' => array(
        'role/index' => 'access_show_roles_list',
        'role/add' => 'access_add_role',
        'role/edit' => 'access_edit_role',
        'role/delete' => 'access_delete_role',
        'permission/index' => 'access_permission_list',
     ),
    'users' => array(
        'user/index' => 'access_show_users_list',
        'user/add' => 'access_add_user',
        'user/edit' => 'access_edit_user',
        'user/delete' => 'access_delete_user',
        'user/profil' => 'access_profil_user',
        'user/showpasswd' => 'access_show_password',
    ),
);
$config['acl'] = $acl;

2 - 创建一个钩子

Hooks/Autorization.php
:

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Authorization {

    private $ci;

    public function __construct(){
        $this->ci = &get_instance();
    }

    public function authorize()
    {
        if (!$this->_has_access()) {
            if ($this->ci->input->is_ajax_request())
                die('-9');

            show_404();
        }
    }

    private function _has_access() {
        $class = $this->ci->router->class;
        $action = $this->ci->router->method;
        $full_action = $class . '/' . $action;
        // --> Start
        $acl = $this->ci->config->item('acl');
        $arr_acl = array();

        array_map(function($value) use (&$arr_acl){
            $arr_acl = array_merge($arr_acl, $value);
        }, array_values($acl));
        // --> End

        if (isset($arr_acl[$full_action])
            && !in_array($full_action, $this->ci->user->permissions))
            return false;

        return true;
    }
}

3 - 通过将

enable_hooks
中的
TRUE
设置为
config/config.php
来激活挂钩:

$config['enable_hooks'] = TRUE;

4 - 设置

Autorization
挂钩,
config/hooks.php
:

$hook['post_controller_constructor'][] = array(
    'class'    => 'Authorization',
    'function' => 'authorize',
    'filename' => 'Authorization.php',
    'filepath' => 'hooks',
    'params'   => array()
);

5 - 添加权限翻译,

language/english/permissions_lang.php

/* ROLE */
$lang['access_show_roles_list'] = "Show all roles.";
$lang['access_add_role'] = "Add new role.";
$lang['access_edit_role'] = "Update a role.";
$lang['access_delete_role'] = "Delete a role.";
$lang['access_change_role_status'] = "Change role stat Enabled/Disabled.";
$lang['access_permission_list'] = "Access to the permissions list.";

6 - 将

acl.php
添加到自动加载文件,在
config.autoload.php
:

$autoload['config'] = array('acl');

就是这样。


1
投票

没有人提到,但您为控制器使用了保留名称。 更改一下,看看是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.