Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.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
Php 如果不在路由中,则CodeIgniter不允许URL_Php_Codeigniter_Codeigniter Url - Fatal编程技术网

Php 如果不在路由中,则CodeIgniter不允许URL

Php 如果不在路由中,则CodeIgniter不允许URL,php,codeigniter,codeigniter-url,Php,Codeigniter,Codeigniter Url,这是我的文件夹结构 |--Application |-----Controllers |--------Dashboard.php |--------Projects.php |-----Models |--------dashboardModel.php |--------projectsModel.php |-----Views |--------dashboard (folder) |-----------index.php |-----------projects (folder) |-

这是我的文件夹结构

|--Application
|-----Controllers
|--------Dashboard.php
|--------Projects.php
|-----Models
|--------dashboardModel.php
|--------projectsModel.php
|-----Views
|--------dashboard (folder)
|-----------index.php
|-----------projects (folder)
|--------------add.php
|--------------index.php
现在,在我的路由文件中,我有以下内容:

$route['default_controller']  = "dashboard";

$route['dashboard/projects']        = "projects";
$route['dashboard/projects/add']    = "projects/add";

现在的问题是:如果我输入url
http://myproject/dashboard/projects
如果我键入
http://myproject/projects
它也可以工作..如何拒绝第二个url?

Codeigniter url映射/路由在以下过程中起作用:

  • 路由数组中是否存在与URI完全匹配的URI
  • 路由数组中是否存在与请求匹配的正则表达式
  • 尝试使用映射“controller[/method[/params]”路由到与请求匹配的控制器
因此,没有设置开关来阻止它从路由到最后一个

必须使用自定义路由器扩展路由器,如下所示:

application/core/
中创建一个名为
MY_Router.php
的文件,该文件将容纳您的自定义路由器,其外观如下:

class My_Router extends CI_Router {
    function _parse_routes()
    {
        // Turn the segment array into a URI string
        $uri = implode('/', $this->uri->segments);

        // Is there a literal match?  If so we're done
        if (isset($this->routes[$uri]))
        {
            return $this->_set_request(explode('/', $this->routes[$uri]));
        }

        // Loop through the route array looking for wild-cards
        foreach ($this->routes as $key => $val)
        {
            // Convert wild-cards to RegEx
            $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));

            // Does the RegEx match?
            if (preg_match('#^'.$key.'$#', $uri))
            {
                // Do we have a back-reference?
                if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
                {
                    $val = preg_replace('#^'.$key.'$#', $val, $uri);
                }

                return $this->_set_request(explode('/', $val));
            }
        }

        // INSTEAD show 404..
        if (count($this->uri->segments) !== 0) {
            show_404();
        }
        else {
            // If we got this far it means we didn't encounter a
            // matching route so we'll set the site default route
            $this->_set_request($this->uri->segments);
        }
    }
}

类的名称取决于
subclass_prefix
config变量设置为什么(默认情况下是MY_u,但您可能已经更改了它..

我知道已经一年了,但想为其他任何人发布一个更简单的选项

您可以始终在config/routes.php的底部沿这些行放置一些内容。它将捕获筛选器未拾取的任何路由,并将它们发送到指定的位置。/auth/invalid生成标准错误消息并返回

这允许我将URL列为白名单,因此用户无法以我可能不打算的方式访问控制器

$route['(:any)/(:any)/(:any)/(:any)'] = '/auth/invalid';
$route['(:any)/(:any)/(:any)'] = '/auth/invalid';
$route['(:any)/(:any)'] = '/auth/invalid';
$route['(:any)'] = '/auth/invalid';

您好,首先谢谢您。我在上看到了该解决方案,我已经尝试过了,但没有完全奏效。我必须添加
$route['dashboard']=“dashboard”;
否则“projects”只能用作“dashboard/projects”,而不能用作“dashboard”如果你想改变核心的行为,这是一条路,你要覆盖哪种方法,以及如何满足你的要求,这取决于你自己,老实说。我只是举个例子。