在codeigniter URL结构中用短划线替换下划线

时间:2013-04-01 15:03:05

标签: php codeigniter

我有一个使用codeigniter开发的网站。我的URL strcuture有大约4个段和500多页。目前我的URL有下划线(_)但我需要替换它破折号( - )

目前的结构

mywebsite.com/app/standalone_apps/high_runner_beta/download

必需的网址结构

mywebsite.com/app/standalone-apps/high-runner-beta/download

我无法使用CI中的路由功能,因为需要重定向的链接太多。

我确实试过了解决方案 Codeigniter Routes regex - using dashes in controller/method names 但它不起作用。

有人可以建议一种方法,我可以用我的网址上的破折号替换下划线。

1 个答案:

答案 0 :(得分:2)

这项工作对我来说 - >创建一个php文件:MY Route.php 使用此代码并上传到“application / core”目录:

<?php class MY_Router extends CI_Router { 
function set_class($class) 
{
    $this->class = str_replace('-', '_', $class);
}

function set_method($method) 
{
    $this->method = str_replace('-', '_', $method);
}

function set_directory($dir) {
    $this->directory = $dir.'/';
}

function _validate_request($segments)
{
    if (count($segments) == 0)
    {
        return $segments;
    }

    // Does the requested controller exist in the root folder?
    if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).'.php'))
    {
        return $segments;
    }

    // Is the controller in a sub-folder?
    if (is_dir(APPPATH.'controllers/'.$segments[0]))
    {
        // Set the directory and remove it from the segment array
        $this->set_directory($segments[0]);
        $segments = array_slice($segments, 1);


        while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
        {
            // Set the directory and remove it from the segment array
            $this->set_directory($this->directory . $segments[0]);
            $segments = array_slice($segments, 1);
        }

        if (count($segments) > 0)
        {
            // Does the requested controller exist in the sub-folder?
            if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).'.php'))
            {
                if ( ! empty($this->routes['404_override']))
                {
                    $x = explode('/', $this->routes['404_override']);

                    $this->set_directory('');
                    $this->set_class($x[0]);
                    $this->set_method(isset($x[1]) ? $x[1] : 'index');

                    return $x;
                }
                else
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
        }
        else
        {
            // Is the method being specified in the route?
            if (strpos($this->default_controller, '/') !== FALSE)
            {
                $x = explode('/', $this->default_controller);

                $this->set_class($x[0]);
                $this->set_method($x[1]);
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');
            }

            // Does the default controller exist in the sub-folder?
            if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
            {
                $this->directory = '';
                return array();
            }

        }

        return $segments;
    }


    // If we've gotten this far it means that the URI does not correlate to a valid
    // controller class.  We will now see if there is an override
    if ( ! empty($this->routes['404_override']))
    {
        $x = explode('/', $this->routes['404_override']);

        $this->set_class($x[0]);
        $this->set_method(isset($x[1]) ? $x[1] : 'index');

        return $x;
    }


    // Nothing else to do at this point but show a 404
    show_404($segments[0]);
} }
相关问题