Case Insensitive Routing in CodeIgniter

前端 未结 8 951
南笙
南笙 2021-01-13 21:46

I\'ve written this in the CodeIgniter\'s routers.

$route[\'companyname\'] = \"/profile/1\";

This is working fine but when I type \"CompanyN

相关标签:
8条回答
  • 2021-01-13 22:20

    Use hooks:

    Create application/hooks/LowerUrl.php

    class LowerUrl {
        public function run() {
                $_SERVER['REQUEST_URI'] = strtolower($_SERVER['REQUEST_URI']);
        }
    }
    

    Add to application/config/hooks.php

    $hook['pre_system'] = array(
        'class'    => 'LowerUrl',
        'function' => 'run',
        'filename' => 'LowerUrl.php',
        'filepath' => 'hooks'
    );
    
    0 讨论(0)
  • 2021-01-13 22:23

    If you want case-insensitive routing for all routes, you just need to extend CI_Router class, then modify _parse_routes() method like this:

    public function _parse_routes()
    {
        foreach ($this->uri->segments as &$segment)
        {
            $segment = strtolower($segment);
        }
    
        return parent::_parse_routes();
    }
    

    It will be cleaner than editing the CI_URI class itself. :)

    0 讨论(0)
提交回复
热议问题