Codeigniter Routes regex - using dashes in controller/method names

前端 未结 9 1261
小鲜肉
小鲜肉 2020-11-29 01:32

I\'m looking for a one line route to route dashed controller and method names to the actual underscored controller and method names.

For example the URL



        
9条回答
  •  半阙折子戏
    2020-11-29 01:55

    Just coming back to this question after upgrading to CodeIgniter 2. Here is a solution which is more robust than the accepted answer because it will survive CodeIgniter core updates.

    _repl($class));
        }
    
        public function set_method($method) 
        {
            parent::set_method($this->_repl($method));
        }
    
        public function _validate_request($segments)
        {
            if (isset($segments[0]))
                $segments[0] = $this->_repl($segments[0]);
            if (isset($segments[1]))
                $segments[1] = $this->_repl($segments[1]);
    
            return parent::_validate_request($segments);
        }
    
        private function _repl($s)
        {
            return str_replace('-', '_', $s);
        }
    }
    

    It should be saved as application/core/MY_Router.php. Now, you can have Controller and Method names with underscores in them such as Abc_Def (in file abc_def.php) and refer to them with the URL /abc-def.

提交回复
热议问题