如何用破折号替换codeigniter url中的下划线?

时间:2022-03-28 08:38:11

I would like to know the simplest solution to changing the underscores of my codeigniter urls to dashes, for seo reasons.

我想知道最简单的解决方案,将我的codeigniter url的下划线更改为短划线,因为seo原因。

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:

但是我想要更加友好,并使用破折号而不是下划线,如下所示:

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???

我用最简单的方法重写url替换下划线的最简单方法是什么?

8 个解决方案

#1


25  

The routes config found in

找到的路由配置

config/routes.php

is your friend here.

你的朋友在这里。

A simple

一个简单的

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

will do this for you.

会为你做这件事。

#2


47  

It really depends on your intention. If you just want to change only one page, then devrooms' solution is the perfect one indeed:

这真的取决于你的意图。如果您只想更改一个页面,那么开发人员的解决方案确实是完美的:

$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中使用连字符而不是下划线])

  1. Make a new file in 'application/core' and name it 'MY_Router.php'
  2. 在'application / core'中创建一个新文件,并将其命名为'MY_Router.php'
  3. 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));
        }
    
    }
    
    ?>
    

UPDATE (Oct 26, 2015): There's a better way to do this in CodeIgniter 3, as @Thomas Wood mentioned:

更新(2015年10月26日):在CodeIgniter 3中有更好的方法,正如@Thomas Wood所说:

$route['translate_uri_dashes'] = TRUE;

#3


26  

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即可使用_或 - 。

Documentation

文档

#4


8  

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_guide controller.

现在,当您访问www.domain.com/request-guide时,它将实例化request_guide控制器。

It will work with all controllers with name containing _ (underscore).

它适用于名称中包含_(下划线)的所有控制器。

#5


2  

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

#6


1  

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

#7


1  

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钩子的更多信息 - 扩展框架核心

/*
 * 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.php file 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);
    }

}

#8


0  

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();
}

#1


25  

The routes config found in

找到的路由配置

config/routes.php

is your friend here.

你的朋友在这里。

A simple

一个简单的

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

will do this for you.

会为你做这件事。

#2


47  

It really depends on your intention. If you just want to change only one page, then devrooms' solution is the perfect one indeed:

这真的取决于你的意图。如果您只想更改一个页面,那么开发人员的解决方案确实是完美的:

$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中使用连字符而不是下划线])

  1. Make a new file in 'application/core' and name it 'MY_Router.php'
  2. 在'application / core'中创建一个新文件,并将其命名为'MY_Router.php'
  3. 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));
        }
    
    }
    
    ?>
    

UPDATE (Oct 26, 2015): There's a better way to do this in CodeIgniter 3, as @Thomas Wood mentioned:

更新(2015年10月26日):在CodeIgniter 3中有更好的方法,正如@Thomas Wood所说:

$route['translate_uri_dashes'] = TRUE;

#3


26  

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即可使用_或 - 。

Documentation

文档

#4


8  

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_guide controller.

现在,当您访问www.domain.com/request-guide时,它将实例化request_guide控制器。

It will work with all controllers with name containing _ (underscore).

它适用于名称中包含_(下划线)的所有控制器。

#5


2  

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

#6


1  

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

#7


1  

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钩子的更多信息 - 扩展框架核心

/*
 * 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.php file 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);
    }

}

#8


0  

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();
}