簡體   English   中英

在codeigniter URL結構中用短划線替換下划線

[英]replace underscores with dashes in codeigniter URL structure

我有一個使用codeigniter開發的網站。 我的URL strcuture有大約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正則表達式中嘗試了解決方案- 在控制器/方法名稱中使用破折號,但它不起作用。

有人可以建議一種方法,我可以用我的網址上的破折號替換下划線。

這項工作對我來說 - >使用此代碼創建一個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]);
} }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM