首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用codeIgniter链接URL中的"-“

使用codeIgniter链接URL中的"-“
EN

Stack Overflow用户
提问于 2011-11-21 18:59:43
回答 3查看 146关注 0票数 0

我(第一次)使用Codeigniter。我想有一个网页的URL "web-design",这样它就可以像/web-design访问。我知道用"-“来命名类名是不可能的,所以我试着用.htaccess来实现,就像这样:

代码语言:javascript
复制
RewriteEngine on

RewriteRule ^web-design$ index.php/webdesign [L]

RewriteCond $1 !^(index\.php|images|robots\.txt|assets)
RewriteRule ^(.*)$ index.php/$1 

但它不起作用。它给了我404错误。我怎么才能让它工作呢?谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-11-21 19:22:15

您应该在CI中使用路由!这就是他们成功的原因。

这实际上非常简单。

application/config/routes.php中,将以下内容添加到$route阵列:

代码语言:javascript
复制
$route['web-design'] = "webdesign";
$route['web-design/(:any)'] = "webdesign/$1";

然后,您可以创建一个名为Webdesign的控制器;问题就解决了--以正确的方式。不需要扩展任何东西或创建其他重写规则。

票数 2
EN

Stack Overflow用户

发布于 2011-11-21 19:14:26

我使用了一个扩展的Router类,它将把任何带有连字符的url转换为下划线。

例如,www.mysite/web-design将被路由到*web_design*,或者www.mysite/ home /want you- will /2将被路由到主控制器,并运行*whatever_you_want*方法/函数,将2作为参数传递。

如果您使用的是Codeigniter2,请将其放入/application/core (我确信您的配置中将前缀设置为_MY__。

代码语言:javascript
复制
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {

    function set_class($class) {
        $this->class = str_replace('-', '_', $class);
    }

    function set_method($method) {
        $this->method = str_replace('-', '_', $method);
    }

    function _validate_request($segments) {
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).EXT)) {
            return $segments;
        }
        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0])) {       
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            if (count($segments) > 0) {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).EXT)) {
                    show_404($this->fetch_directory().$segments[0]);
                }
            } else {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        // Can't find the requested controller...
        show_404($segments[0]);
    }
}

请注意,这不是我的代码,但我不记得我在哪里找到它的,所以如果它是你的-夸奖!

票数 2
EN

Stack Overflow用户

发布于 2011-11-21 19:04:08

-是正则表达式中的一个特殊字符。尝试转义它,如下所示:

RewriteRule ^web\-design$ index.php/webdesign [L]

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8210684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档