当前位置: 首页 > 面试题库 >

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

韦飞尘
2023-03-14
问题内容

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

例如网址

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

将路由到

/controller_name/method_name_which_is_long/

参见: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]);
    }
}

现在,您可以自由使用网址,例如http://example.com/logued/presse-
access
,它将通过自动将破折号转换为下划线来调用适当的控制器和功能。

编辑: 这是我们的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_';在application / config / 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中调用它。如果不需要,可以删除此代码,但对正常流程没有危害。



 类似资料:
  • 问题内容: 刚刚开始探索正则表达式的“奇迹”。作为一个从试验和错误中学习的人,我真的很努力,因为我的试验抛出了不成比例的错误……我的实验是在PHP中使用ereg()进行的。 无论如何。我分别使用名字和姓氏,但现在使用相同的正则表达式。到目前为止,我有: 任何以大写字母开头且其余仅包含字母(大写或不大写)的长度字符串。但是我分崩离析的地方是在几乎任何地方都可能发生的特殊情况下。 连字符(Worthi

  • 问题内容: 我正在尝试建立一个请求过滤器,该过滤器仅在与字母e和数字的模式匹配时才会使用。但是我似乎无法使其正常工作。每次尝试使用正则表达式时,我都会不断收到400个错误。 如果仅使用以下内容,它“有效”,但还会捕获没有我不需要的数字的映射。 我尝试了以下组合。 问题答案: 根据文档,您必须使用。甚至有一个例子:

  • 问题内容: 因为我想分离系统的前端和后端。我已经在控制器内部创建了2个文件夹作为前端和后端 下面是我的控制器文件夹的结构 我可以通过使用以下功能 但我想从网址中删除前端和后端段。 我检查了codeigniter中的路由功能,但据我所知,我需要分别指定每个路由。由于我大约有12个控制器,每个控制器都具有大约10 -15个功能,因此我可能必须指定该路由的每个功能。 有没有其他有效的方法可以实现使用路由

  • 问题内容: 我要备份数据库时出现错误 有什么办法可以用表名(temp_01-01-000001)解决这个问题。 问题答案: 您可以编辑文件/system/database/drivers/mysql/mysql_utility.php的第132行 从: 至:

  • 问题内容: 我试图用下划线替换方括号内的破折号,但它用字符串中的下划线替换所有破折号。 例如,我要替换 与 但是它将下划线替换字符串中的 所有 破折号。 问题答案: 您可以使用 至于正则表达式本身,只有在符号后跟非s和非s的情况下,我才会匹配该符号,直到引擎找到为止。然后,我们在s中。可能情况并非如此(第4个连字符),但我希望这不是您的情况。 IDEONE演示 输出:

  • 问题内容: 我对正则表达式不太满意,因此我想确保自己正确执行了此操作。假设我有两条非常相似的路线,和。我想创建一个匹配这两个页面的路由。 这是正确的方法吗?现在,我只是在创建两条单独的路线。 问题答案: 作品