使用Codeigniter将网址更改为短划线而不是下划线

时间:2014-02-27 12:30:02

标签: codeigniter url

您好我正在使用codeigniter框架,因为它涉及到MVC的东西真的很酷。我在网址上有一个问题。通常在保存控制器时,让我们在about us页面中说它必须做这样的事情About_us扩展CI_Controller。当它出现在网址上时,就像这个test.com/about_us。我希望我的网址不是下划线。我想要像这个test.com/about-us这样破灭。我怎样才能使用破折号而不是使用下划线???

非常感谢任何帮助 谢谢!

2 个答案:

答案 0 :(得分:1)

你只需要为你的页面创建路由,据我所知,没有配置指令来更改分隔字符,或者在CI_Router中用'_'替换' - ',(可能不会太难添加了这个。)

允许使用' - '而不是'_':

在application / core / MY_Router.php中创建一个文件

将'MY_'更改为配置指令中指定的任何值:$config['subclass_prefix']

插入代码:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Router extends CI_Router {
  function _set_request($segments = array()) {
    if (isset($segments[0]))
      $segments[0] = str_replace('-','_',$segments[0]);

    if (isset($segments[1])) {
      $segments[1] = str_replace('-','_',$segments[1]);
    }

    return parent::_set_request($segments);
  }
}

答案 1 :(得分:0)

Code Ignitor 3具有此内置选项。在您的routes.php中:

$route['translate_uri_dashes'] = FALSE;

只需将此更改为&#34; TRUE&#34;您可以在网址

中使用_-

Here is a detailed article how to fix it