php 如何用破折号替换codeigniter url中的下划线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8052245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to replace underscores in codeigniter url with dashes?
提问by hairynuggets
I would like to know the simplest solution to changing the underscores of my codeigniter urls to dashes, for seo reasons.
由于 seo 原因,我想知道将我的 codeigniter url 的下划线更改为破折号的最简单解决方案。
My controllers look like:
我的控制器看起来像:
public function request_guide() {
...Load view etc...
}
So to browse to this page I would have to go to:
所以要浏览到这个页面,我必须去:
www.domain.com/request_guide
But I want to be more seo friendly and use dashes instead of underscores, like so:
但我想更加 seo 友好并使用破折号而不是下划线,如下所示:
www.domain.com/request-guide
I am under the impression that codeigniter functions require underscores (might be wrong).
我的印象是 codeigniter 函数需要下划线(可能是错误的)。
In previous projects I have simply used mod_rewrite but I believe that these actions can be performed using routes.
在以前的项目中,我只是使用了 mod_rewrite,但我相信这些操作可以使用路由来执行。
What is the easiest way for me to rewrite the urls replacing the underscores with dashes???
我用破折号替换下划线的最简单方法是什么?
回答by Stan
It really depends on your intention. If you just want to change only one page, then devrooms' solution is the perfect one indeed:
这真的取决于你的意图。如果您只想更改一页,那么devrooms的解决方案确实是完美的解决方案:
$route['request-guide'] = "request_guide";
But if you want to make this your website's default behavior you should extend your core Router class like this (source:[Using hyphens instead of underscores in CodeIgniter])
但是如果你想让它成为你网站的默认行为,你应该像这样扩展你的核心路由器类(来源:[在 CodeIgniter 中使用连字符而不是下划线])
- Make a new file in 'application/core' and name it 'MY_Router.php'
Insert this code in it:
<?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)); } } ?>
- 在“application/core”中创建一个新文件并将其命名为“MY_Router.php”
在其中插入以下代码:
<?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)); } } ?>
UPDATE (Oct 26, 2015): There's a better way to do this in CodeIgniter 3, as @Thomas Wood mentioned:
更新(2015 年 10 月 26 日):正如@Thomas Wood 提到的那样,在 CodeIgniter 3 中有更好的方法来做到这一点:
$route['translate_uri_dashes'] = TRUE;
回答by Thomas Wood
Code Ignitor 3 has this in built:
Code Ignitor 3 内置了这个:
$route['translate_uri_dashes'] = FALSE;
$route['translate_uri_dashes'] = FALSE;
Just change to TRUE
and you can use either _
or -
.
只需更改为TRUE
,您就可以使用_
或-
。
回答by devrooms
The routes config found in
在中找到的路由配置
config/routes.php
is your friend here.
是你的朋友吗?
A simple
一个简单的
$route['request-guide'] = "request_guide" ;
will do this for you.
会为你做这件事。
回答by Usman Ahmed
Open application/config/routes.php and change
打开 application/config/routes.php 并更改
$route['translate_uri_dashes'] = TRUE;
That is it you need to do.
这就是你需要做的。
Now when you access www.domain.com/request-guide, it will instantiate request_guidecontroller.
现在,当你访问www.domain.com/请求指导,它将实例request_guide控制器。
It will work with all controllers with name containing _ (underscore).
它将与名称包含 _(下划线)的所有控制器一起使用。
回答by Andrei Serdeliuc ?
Have a look at Codeigniter's custom routing http://codeigniter.com/user_guide/general/routing.html
看看 Codeigniter 的自定义路由http://codeigniter.com/user_guide/general/routing.html
回答by luckytaxi
$route['request-guide'] = "request_guide";
回答by Waqleh
What you could do is create a custom hook (PST... you need basic CodeIgniter skills): for more information regarding CodeIgniter Hooks - Extending the Framework Core
您可以做的是创建一个自定义钩子(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']);
}
I named the file customhooks.php.
我将文件命名为customhooks.php。
Then add this to the hooks.php file in application/config:
然后将其添加到application/config 中的 hooks.php 文件:
$hook['pre_system'] = array(
'class' => '',
'function' => 'prettyurls',
'filename' => 'customhooks.php',
'filepath' => 'hooks',
'params' => array()
);
You will need to edit your application/config/config.phpfile to enable hooks
您需要编辑application/config/config.php文件以启用钩子
$config['enable_hooks'] = TRUE;
EXTRA:
额外的:
so that when you use $this->uri->uri_string()it stays hyphenated do the following Creating Core System Classes
这样当您使用$this->uri->uri_string() 时,它会保持连字符,请执行以下操作创建核心系统类
class MY_URI extends CI_URI {
function uri_string() {
return str_replace('_', '-', $this->uri_string);
}
}
回答by Sajjad Ashraf
You can use this _remap() method to handle such behavior. Place this method in your controllers or create a core controller and place it in.
您可以使用此 _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();
}