使用 Codeigniter 将 url 更改为破折号而不是下划线

问题描述 投票:0回答:2

嗨,我正在使用 codeigniter 框架,因为它在 MVC 方面真的很酷。我有一个关于 url 的问题。通常,在保存控制器时,假设在“关于我们”页面中它必须执行类似“About_us extends CI_Controller”的操作。当涉及到 url 时,它就像这样 test.com/about_us。我希望我的网址不带下划线。我想要像 test.com/about-us 这样的东西被破折号。我如何才能使用破折号而不是下划线???

非常感谢任何帮助 谢谢!

codeigniter url
2个回答
2
投票

Code Ignitor 3 有这个内置选项。在你的routes.php中:

$route['translate_uri_dashes'] = FALSE;

只需将其更改为“TRUE”,您就可以在 URL 中使用

_
-

这里有一篇详细的文章如何修复它


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);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.