Codeigniter 路由正则表达式 - 在控制器/方法名称中使用破折号

2024-01-11

我正在寻找一种单行路由,将虚线控制器和方法名称路由到实际的下划线控制器和方法名称。

例如网址

/controller-name/method-name-which-is-long/

将路由至

/controller_name/method_name_which_is_long/

see: http://codeigniter.com/forums/viewreply/696690/ http://codeigniter.com/forums/viewreply/696690/这让我想到要问:)


这也正是我的要求,我正在使用类似的路线

$route['logued/presse-access'] = "logued/presse_access";

在我之前的项目中,我需要创建 300-400 个路由规则,其中大部分是由于破折号到下划线的转换。

对于我的下一个项目,我迫切希望避免它。我已经做了一些快速的破解并测试了它,虽然没有在任何实时服务器中使用,但它对我有用。请执行下列操作..

确保您的 system/application/config/config.php 中的 subclass_prefix 如下

$config['subclass_prefix'] = 'MY_';

然后在 system/application/libraries 目录中上传名为 MY_Router.php 的文件。

<?php

class MY_Router extends CI_Router { 
    function set_class($class) 
    {
        //$this->class = $class;
        $this->class = str_replace('-', '_', $class);
        //echo 'class:'.$this->class;
    }

    function set_method($method) 
    {
//      $this->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]);
    }
}

现在你可以自由地使用 url 像http://example.com/loged/presse-access http://example.com/logued/presse-access它会通过自动将破折号转换为下划线来调用正确的控制器和函数。

Edit:这是我们的 Codeigniter 2 解决方案,它覆盖了新的 CI_Router 函数:

<?php

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 set_directory($dir) {
        $this->directory = $dir.'/';
    }

    function _validate_request($segments)
    {
        if (count($segments) == 0)
        {
            return $segments;
        }

        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).'.php'))
        {
            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);


            while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
            {
                // Set the directory and remove it from the segment array
                $this->set_directory($this->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]).'.php'))
                {
                    if ( ! empty($this->routes['404_override']))
                    {
                        $x = explode('/', $this->routes['404_override']);

                        $this->set_directory('');
                        $this->set_class($x[0]);
                        $this->set_method(isset($x[1]) ? $x[1] : 'index');

                        return $x;
                    }
                    else
                    {
                        show_404($this->fetch_directory().$segments[0]);
                    }
                }
            }
            else
            {
                // Is the method being specified in the route?
                if (strpos($this->default_controller, '/') !== FALSE)
                {
                    $x = explode('/', $this->default_controller);

                    $this->set_class($x[0]);
                    $this->set_method($x[1]);
                }
                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.'.php'))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }


        // If we've gotten this far it means that the URI does not correlate to a valid
        // controller class.  We will now see if there is an override
        if ( ! empty($this->routes['404_override']))
        {
            $x = explode('/', $this->routes['404_override']);

            $this->set_class($x[0]);
            $this->set_method(isset($x[1]) ? $x[1] : 'index');

            return $x;
        }


        // Nothing else to do at this point but show a 404
        show_404($segments[0]);
    }
}

现在必须将此文件放置在 application/core/MY_Router.php 中,并确保其 subclass_prefix 定义为$config['subclass_prefix'] = 'MY_';在应用程序/配置/config.php

方法中添加了几行额外的代码_validate_request():

while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
{
    // Set the directory and remove it from the segment array
    $this->set_directory($this->directory . $segments[0]);
    $segments = array_slice($segments, 1);
}

使用它是为了可以使用controllers目录中的多层子目录,而通常我们可以使用controllers文件夹中的单级子目录,并可以在url中调用它。如果不需要,可以删除此代码,但对正常流程没有损害。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Codeigniter 路由正则表达式 - 在控制器/方法名称中使用破折号 的相关文章

  • 在会话 cookie 中存储大量数据会产生什么影响?

    谁能解释一下在会话中存储大量数据的缺点或给我指出一些阅读材料 我也很感兴趣在会话中存储数据和从数据文件读取数据之间是否有任何区别 如果您在会话中存储大量数据 则输入 输出性能会下降 因为会有大量读取 写入 默认情况下 PHP 中的会话存储在
  • 使用 rspec 测试嵌套路由

    我正在尝试使用 rspec 测试路由 以下给出了 预期块返回真值 的错误 我不确定我错过了什么 通过浏览器我可以发布到这个网址并且成功 有任何想法吗 谢谢 Routes resources forum topics do resources
  • 关于加拿大短信网关提供商的建议[关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我很好奇 如果我能够接受传入的短信到某个号码 然后将其传递给 PHP 中的服务器端应用程序 会带来多少麻烦 金钱 我最终会通过电子邮件地址发回短信 有
  • 如何在 Zend Framework 中存储 cron 作业的脚本?

    因为 ZF 的所有 URL 都依赖于 mod 重写 所以我并不清楚应该在哪里存储用于 cron 作业的本地脚本 有人有什么建议 或者有 正式接受 的方式吗 我用模块化目录结构 http framework zend com manual e
  • 如何在MAMP中设置环境变量?

    如何在 MAMP 版本 3 3 中设置环境变量 我可以在我的 PHP 应用程序中使用它 我已经更新了 Applications MAMP Library bin envvars and envvars std file并添加以下行 Lice
  • 使用 PHP 将值插入可编辑 PDF,并保持可编辑状态

    我有一个带有可编辑字段的 PDF 我希望将 HTML 表单中的值传递到此 PDF 中 我尝试过使用 FPDF 并且它有效 但是将值传递到 PDF 后 pdf 中的字段不再可编辑 另一个缺点是 在将值传递到 PDF 时 我们必须为每个字段指定
  • 如何将 PHPMailer 与 Codeigniter 3 集成

    嗨 我正在尝试使用PHPMailer 库 https github com PHPMailer PHPMailer来自我的 Codeigniter 应用程序中的 GitHub 我下载了代码并解压到我的application library文
  • strlen()==0 和empty()之间有区别吗?

    我正在查看其他人编写的一些表单验证代码 我看到了这个 strlen 0 当测试表单变量是否为空时 我使用empty 功能 一种方法比另一种方法更好吗 它们在功能上等效吗 strlen是获取字符串中的字符数 同时empty用于测试变量是否为空
  • php 中的简单授权/登录功能

    我希望第一次实现用户登录到我的网站 我很高兴构建自己的解决方案 或者实现一些开源的东西 但是到目前为止 在我的搜索中没有任何包是明显的选择 同样 我完全意识到 作为一名中级 php 程序员 如果我推出自己的解决方案 并真正敞开大门 我很可能
  • 运行PHPUnit测试时如何避免内部调用函数?以及如何设置内部性能的模拟数据?

    我有一个类 Receipt php
  • 使用日语“Enter”键进行搜索功能

    我在日语方面遇到了问题 我有一个允许用户搜索数据的表单 当用户输入要搜索的字符串并按 Enter 键时 搜索功能就会执行 我的代码是 formSearch input keyup function event var key event c
  • 字符串中的注释和注释中的字符串

    我正在尝试使用 Python 和 Regex 计算 C 代码中包含的注释中的字符数 但没有成功 我可以先删除字符串以删除字符串中的注释 但这也会删除注释中的字符串 结果会很糟糕 是否有机会通过使用正则表达式来询问不匹配注释中的字符串 反之亦
  • 使用PHP套接字发送和接收数据

    我正在尝试通过 PHP 套接字发送和接收数据 一切正常 但是当我尝试发送数据时 PHP 不发送任何内容 Wireshark 告诉我发送的数据长度为 0 我正在使用这段代码
  • 如何用javascript正确读取php cookies

    考虑这个 php 和 javascript 代码 然后我在控制台中看到的是 utma 111872281 291759993 1444771465 1445374822 1445436904 4 utmz 111872281 1444771
  • 无法实例化模块 [$injector:unpr] 未知提供程序:$routeProvider

    我从 AngularJS 升级时收到此错误1 0 7 to 1 2 0rc1 ngRoute 模块不再是核心的一部分angular js文件 如果您继续使用 routeProvider 那么您现在需要包括angular route js在你
  • php 表单提交 - Q2

    我对这个虚拟问题感到抱歉 这是我的简单 PHP 表单 其中包含两个 SQL 表和 ADD 提交 按钮 我希望将人员从 Test1 转移到 Test2 很多事情都很好 只有提交按钮不起作用 因此 Test2 表没有反馈 Revised 现在提
  • 如何在 codeigniter 查询中使用 FIND_IN_SET?

    array array classesID gt 6 this gt db gt select gt from this gt table name gt where array gt order by this gt order by q
  • PHP cURL 在本地工作,在 AWS 服务器上出现错误 77

    最新更新 脚本作为管理员用户通过 SSH shell 作为 php script php 成功运行 当由 nginx 用户运行时 curl 命令无法执行 https 请求 所以我猜测这是nginx用户无法正确使用curl的问题 我已经检查了
  • PHP 和 NLP:嵌套括号(解析器输出)到数组?

    想要将带有嵌套括号的文本转换为嵌套数组 以下是 NLP 解析器的输出示例 TOP S NP PRP I VP VBP love NP NP DT a JJ big NN bed PP IN of NP NNS roses 原文 我喜欢一大床
  • 在 PHP 中模拟 jQuery.ajax 请求

    我必须在 PHP 中模拟 AJAX 请求 就像在 jQuery 中一样 我当前的代码在这里 原始 AJAX 调用 不得修改 ajax type POST url someFile php data data success function

随机推荐