Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
codeigniter路由和重定向_Codeigniter_Codeigniter Routing - Fatal编程技术网

codeigniter路由和重定向

codeigniter路由和重定向,codeigniter,codeigniter-routing,Codeigniter,Codeigniter Routing,当我输入我的基本urlhttp://localhost/myproject/admin它不断将我发送到我的权限页面。http://localhost/myproject/admin是基本url() mycore/Controller.php的工作原理是,它检查是否可以访问控制器,如果不在忽略列表中,则重定向到权限,否则将访问页面 我想知道的是,如果有可能,如何也添加我的base\u url(),这样它就会忽略它,并允许我访问它。我不确定在下面的代码中添加它的最佳位置。 <?php if (

当我输入我的基本url
http://localhost/myproject/admin
它不断将我发送到我的权限页面。
http://localhost/myproject/admin
是基本url()

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

我想知道的是,如果有可能,如何也添加我的
base\u 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');
        }
    }
}
}

使用钩子检查权限:

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;
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;
    }
}
$hook['post_controller_constructor'][] = array(
    'class'    => 'Authorization',
    'function' => 'authorize',
    'filename' => 'Authorization.php',
    'filepath' => 'hooks',
    'params'   => array()
);
/* 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.";
$autoload['config'] = array('acl');
3-通过在
config/config.php
中将
enable_hooks
设置为
TRUE
来激活钩子:

$config['enable_hooks'] = TRUE;
4-设置
自动化
hook,
config/hooks.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;
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;
    }
}
$hook['post_controller_constructor'][] = array(
    'class'    => 'Authorization',
    'function' => 'authorize',
    'filename' => 'Authorization.php',
    'filepath' => 'hooks',
    'params'   => array()
);
/* 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.";
$autoload['config'] = array('acl');
5-添加权限的翻译,
language/english/permissions\u lang.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;
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;
    }
}
$hook['post_controller_constructor'][] = array(
    'class'    => 'Authorization',
    'function' => 'authorize',
    'filename' => 'Authorization.php',
    'filepath' => 'hooks',
    'params'   => array()
);
/* 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.";
$autoload['config'] = array('acl');
6-将
acl.php
添加到自动加载文件中的
config.autoload.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;
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;
    }
}
$hook['post_controller_constructor'][] = array(
    'class'    => 'Authorization',
    'function' => 'authorize',
    'filename' => 'Authorization.php',
    'filepath' => 'hooks',
    'params'   => array()
);
/* 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.";
$autoload['config'] = array('acl');

就这样。

除了控制器之外,没有人提到过你。
更改它并查看是否有效。

我已经准备好配置路由和默认控制器。有更好的方法,比如使用包含所有路由的配置文件挂钩,或者使用
\u remap()
函数。无论如何,您可以将
admin/index
添加到您的
$ignore
阵列中。您更喜欢上面的选项。Beacue我还有其他权限将添加到其中,除非您可以展示如何在钩子中执行的示例。当然,请检查我的回答我确信您已配置路由,但如果您想对特定情况执行其他覆盖,路由仍应能够使用
(:any)实现这一点
和索引URI参数
$1/$2
等告诉您需要的其他权限存储在会话中的_数组
$this->session->userdata('access')
中,而不是手动键入$acl。我可以在_数组中添加($this->session->userdata('access'),如图所示var dump会话只需查看我忘记提到的var dump会话,数据库中存储了角色和权限的一部分,当用户登录时,您将获取该用户角色的所有权限
$this->user->permissions
我不需要配置部分我的所有访问权限都存储在该会话数组中我如何使用它您可以尝试:
!在数组中($full\u action,$this->ci->session->userdata('access'))返回false
不起作用,因为事实上仍然需要配置项。我知道我试过了,但希望所有扩展类都相同。