簡體   English   中英

如何用破折號替換codeigniter url中的下划線?

[英]How to replace underscores in codeigniter url with dashes?

由於 seo 原因,我想知道將我的 codeigniter url 的下划線更改為破折號的最簡單解決方案。

我的控制器看起來像:

public function request_guide() {
...Load view etc...
}

所以要瀏覽到這個頁面,我必須去:

www.domain.com/request_guide

但我想更加 seo 友好並使用破折號而不是下划線,如下所示:

www.domain.com/request-guide

我的印象是 codeigniter 函數需要下划線(可能是錯誤的)。

在以前的項目中,我只是使用了 mod_rewrite,但我相信這些操作可以使用路由來執行。

我用破折號替換下划線的最簡單方法是什么?

這真的取決於你的意圖。 如果您只想更改一頁,那么devrooms的解決方案確實是完美的解決方案:

$route['request-guide'] = "request_guide";

但是如果你想讓它成為你網站的默認行為,你應該像這樣擴展你的核心路由器類(來源: [在 CodeIgniter 中使用連字符而不是下划線]

  1. 在“application/core”中創建一個新文件並將其命名為“MY_Router.php”
  2. 在其中插入以下代碼:

     <?php defined('BASEPATH') || exit('No direct script access allowed'); class MY_Router extends CI_Router { function _set_request ($seg = array()) { // The str_replace() below goes through all our segments // and replaces the hyphens with underscores making it // possible to use hyphens in controllers, folder names and // function names parent::_set_request(str_replace('-', '_', $seg)); } } ?>

更新(2015 年 10 月 26 日) :正如@Thomas Wood 提到的那樣,在 CodeIgniter 3 中有更好的方法來做到這一點:

$route['translate_uri_dashes'] = TRUE;

Code Ignitor 3 內置了這個:

$route['translate_uri_dashes'] = FALSE;

只需更改為TRUE即可使用_-

文檔

在中找到的路由配置

config/routes.php

是你的朋友嗎?

一個簡單的

$route['request-guide'] = "request_guide" ;

會為你做這件事。

打開 application/config/routes.php 並更改

$route['translate_uri_dashes'] = TRUE;

現在,當您訪問www.domain.com/**request-guide** 時,它將實例化request_guide控制器。

它將與名稱包含 _(下划線)的所有控制器一起使用。

看看 Codeigniter 的自定義路由http://codeigniter.com/user_guide/general/routing.html

$route['request-guide'] = "request_guide";

您可以做的是創建一個自定義鈎子(PST ...您需要基本的 CodeIgniter 技能):有關 CodeIgniter Hooks - Extending the Framework Core 的更多信息

/*
 * the hooks must be enabled from the config file
 * replace underscore with dashes (hyphens) for SEO
 */

function prettyurls() {
    if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '') {
        $newkey = str_replace('-', '_', key($_GET));
        $_GET[$newkey] = $_GET[key($_GET)];
        unset($_GET[key($_GET)]);
    }
    if (isset($_SERVER['PATH_INFO']))
        $_SERVER['PATH_INFO'] = str_replace('-', '_', $_SERVER['PATH_INFO']);
    if (isset($_SERVER['QUERY_STRING']))
        $_SERVER['QUERY_STRING'] = str_replace('-', '_', $_SERVER['QUERY_STRING']);
    if (isset($_SERVER['ORIG_PATH_INFO']))
        $_SERVER['ORIG_PATH_INFO'] = str_replace('-', '_', $_SERVER['ORIG_PATH_INFO']);
    if (isset($_SERVER['REQUEST_URI']))
        $_SERVER['REQUEST_URI'] = str_replace('-', '_', $_SERVER['REQUEST_URI']);
}

我將文件命名為customhooks.php

然后將其添加到application/config 中的 hooks.php 文件:

$hook['pre_system'] = array(
    'class' => '',
    'function' => 'prettyurls',
    'filename' => 'customhooks.php',
    'filepath' => 'hooks',
    'params' => array()
);

您需要編輯application/config/config.php文件以啟用鈎子

$config['enable_hooks'] = TRUE;

額外的:

這樣當您使用$this->uri->uri_string() 時,它會保持連字符,請執行以下操作創建核心系統類

class MY_URI extends CI_URI {

    function uri_string() {
        return str_replace('_', '-', $this->uri_string);
    }

}

您可以使用此 _remap() 方法來處理此類行為。 將此方法放在您的控制器中或創建一個核心控制器並將其放入。

public function _remap($method, $params = array()){
    if(method_exists($this, $method)){
        return call_user_func_array(array($this, $method), $params);
    }else{
        $method = str_replace("-", "_", $method);
        if(method_exists($this, $method)){
            return call_user_func_array(array($this, $method), $params);
        }
    }
    show_404();
}

暫無
暫無

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

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