0

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

当前结构

mywebsite.com/app/standalone_apps/high_runner_beta/download

所需的 URL 结构

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

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

我确实尝试了 Codeigniter Routes 正则表达式中的解决方案 - 在控制器/方法名称中使用破折号, 但它不起作用。

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

4

1 回答 1

2

这对我有用-> 使用此代码创建一个 php 文件:MY_Route.php 并上传到“应用程序/核心”目录:

<?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]);
} }
于 2013-10-24T00:14:45.403 回答