Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CodeIgniter和友好的URL_Codeigniter_Friendly Url - Fatal编程技术网

CodeIgniter和友好的URL

CodeIgniter和友好的URL,codeigniter,friendly-url,Codeigniter,Friendly Url,我目前有一组url,它们是从它们的控制器名称派生出来的,但它们不是非常url友好的 例如,我是否有任何更改: example.com/admin/news\u manager/add\u article 到 example.com/admin/news-manager/add-article 任何帮助都将不胜感激!谢谢。您可以使用URI路由。请参阅CodeIgniter文档中的页面。这是我所拥有的,将其作为MY_Router.php放入您的应用程序/核心文件夹中 它会将url中的所有-,转换为u

我目前有一组url,它们是从它们的控制器名称派生出来的,但它们不是非常url友好的

例如,我是否有任何更改:

example.com/admin/news\u manager/add\u article

example.com/admin/news-manager/add-article


任何帮助都将不胜感激!谢谢。

您可以使用URI路由。请参阅CodeIgniter文档中的页面。

这是我所拥有的,将其作为MY_Router.php放入您的应用程序/核心文件夹中

它会将url中的所有-,转换为u

    <?php

class MY_Router extends CI_Router { 
    function set_class($class) 
    {
        $this->class = $this->replace_underscores($class);
    }

    function set_method($method)
    {
        $this->method = $this->replace_underscores($method);
    }

    private function replace_underscores($string){
        return str_replace('-', '_', $string);
    }

    function _validate_request($segments)
    {
        $this->segments = $segments;
        if(empty($this->segments) || $this->controller_is_in_root_folder())
            return $this->segments;

        if ($this->controller_is_in_a_subfolder())
        {
            $this->segments =  $this->set_directory_and_remove_from_array();

            if ($this->at_least_one_segment() > 0 && !$this->default_controller_is_in_subfolder())
            {
                    show_404($this->fetch_directory().$this->segments[0]);
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                if (!$this->default_controller_is_in_subfolder())
                {
                    $this->directory = '';
                    return array();
                }
            }
            return $this->segments;
        }
        else
            show_404($this->segments[0]);
    }

    private function at_least_one_segment(){
        return count($this->segments) >= 1;
    }

    private function controller_is_in_a_subfolder(){
        return is_dir(APPPATH.'controllers/'.$this->segments[0]);
    }

    private function controller_is_in_root_folder(){
        return file_exists(APPPATH.'controllers/'.str_replace('-', '_', $this->segments[0]).EXT);
    }

    private function set_directory_and_remove_from_array(){
        $this->set_directory($this->segments[0]);
        return array_slice($this->segments, 1);
    }

    private function default_controller_is_in_subfolder(){
        return file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $this->segments[0]).EXT);
    }
}

如果这只是一个函数,您可以打开
应用程序/config/routes.php
并添加一行如下内容:

$route['^admin/news-manager/add-article'] = $route['admin/news_manager/add_article'];
根据您的其他URL是什么,您可以提出一个更通用的规则。

相关: