简体   繁体   English

如果不在路由中,CodeIgniter会禁用URL

[英]CodeIgniter disallow URL if not in route

This is my folder structure. 这是我的文件夹结构。

|--Application
|-----Controllers
|--------Dashboard.php
|--------Projects.php
|-----Models
|--------dashboardModel.php
|--------projectsModel.php
|-----Views
|--------dashboard (folder)
|-----------index.php
|-----------projects (folder)
|--------------add.php
|--------------index.php

Now, in my route file I have the following: 现在,在我的路径文件中,我有以下内容:

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

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

Now the problem: if I type in the url http://myproject/dashboard/projects it works AND if I type http://myproject/projects it also works..how do I deny the second url? 现在的问题是:如果我输入网址http://myproject/dashboard/projects它可以工作如果我输入http://myproject/projects它也可以工作..我会拒绝第二个网址吗?

I know it's been a year, but wanted to post for anyone else looking for an easier option. 我知道这已经过了一年,但是想发布给其他寻找更简单选择的人。

You can always put something along these lines at the bottom of config/routes.php. 您总是可以在config / routes.php的底部放置这些内容。 It'll catch any routes that haven't been picked up by a filter, and send them to where you specify. 它将捕获过滤器未拾取的任何路由,并将它们发送到您指定的位置。 /auth/invalid generates a standard error message and returns it. / auth / invalid生成标准错误消息并将其返回。

This allows me to whitelist URLs, so users cant access controllers in a way i might not plan for. 这允许我将URL列入白名单,因此用户无法以我可能不计划的方式访问控制器。

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

Codeigniter URL mapping / routing works on the following process: Codeigniter URL映射/路由适用于以下过程:

  • Does an exact match for the URI exist in the routes array? 路由数组中是否存在与URI的完全匹配?
  • Does a regex exist in the route array which matches the request? 路由数组中是否存在与请求匹配的正则表达式?
  • Try route to a controller matching the request using map: "controller[/method[/params]]" 尝试使用map路由到匹配请求的控制器:“controller [/ method [/ params]]”

So there is no setting to switch to stop it from routing to the last one... 因此,没有设置可以切换到停止路由到最后一个...

you must extend the Router with a custom one like so: 您必须使用自定义方式扩展路由器,如下所示:

in application/core/ create a file called MY_Router.php this will house your custom router, and it will look something like this: 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);
        }
    }
}

the name of your class depends on what the subclass_prefix config variable is set to (by default its MY_ but you may have changed it.. 您的类的名称取决于subclass_prefix配置变量设置为什么(默认情况下它的MY_,但您可能已经更改它..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM