【问题标题】:How can i call controller inside sub folder in codeigniter我如何在codeigniter的子文件夹中调用控制器
【发布时间】:2015-05-31 15:39:48
【问题描述】:

谁能告诉我如何在子文件夹中调用控制器。我正在尝试在控制器文件夹的第三级调用控制器,我通过谷歌搜索尝试了一些示例,但所有这些都不起作用。 例如:

http://localhost/project/index.php?/folder1/folder2/folder3/controller.php

【问题讨论】:

标签: php codeigniter


【解决方案1】:

applicaiton/core 文件夹中创建名为 MY_Router.php 的文件,并将下面的代码放入其中,它将起作用:--

注意:此解决方案适用于 Codeigniter 2.2.0

<?php


Class MY_Router extends CI_Router
{
    Function MY_Router()
    {
        parent::CI_Router();
    }

    function _validate_request($segments)
    {
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            /* ----------- ADDED CODE ------------ */

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

            /* ----------- END ------------ */

            if (count($segments) > 0)
            {
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        show_404($segments[0]);
    }
}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    相关资源
    最近更新 更多