8

我在我的控制器目录中设置了一个管理文件夹,在该文件夹下我有 3 个单独的子文件夹,其中包含控制器。

-- Controllers
---- Admin
------ Dashboard
-------- dashboard.php
-------- file.php
------ Members
-------- members.php
-------- file.php
------ Settings
-------- settings.php
-------- file.php

我尝试像这样在 routes.php 文件中路由它

$route['admin/(:any)/(:any)'] = 'admin/$1/$2';
$route['admin/(:any)'] = 'admin/$1/$1';
$route['admin'] = 'admin/index';

我该怎么做才能解决这个问题?

4

4 回答 4

11

该代码已经在互联网上,但我对其进行了修改以使其适用于 codeigniter 2.1

在此处查看旧源:http: //glennpratama.wordpress.com/2009/10/20/multi-level-subfolder-for-controller-in-codeigniter/

在 application/core 目录下新建一个文件 MY_Router.php,复制以下代码:

<?php

/*
 * Custom router function v 0.2
 *
 * Add functionality : read into more than one sub-folder
 *
 */

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

    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]);
            if (substr($this->directory, -1, 1) == '/')
                $this->directory = $this->directory . $segments[0];
            else
                $this->directory = $this->directory . '/' . $segments[0];

            $segments = array_slice($segments, 1);
            }

            if (substr($this->directory, -1, 1) != '/')
                $this->directory = $this->directory . '/';

            /* ----------- 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]);
    }
}
于 2012-12-06T21:01:39.503 回答
5

“开箱即用”codeigniter 不支持控制器目录中的多个子目录级别,仅支持一个。

有一种方法可以扩展路由类来支持这一点,请查看此博客条目。

于 2011-03-06T22:37:32.617 回答
3

对于 Codeigniter 3.x 兼容性:自从放弃对 PHP 4 的支持以来,已弃用 EXT 常量。不再需要维护不同的文件扩展名,在这个新的 CodeIgniter 版本 (3.x) 中,EXT 常量已经删除。只使用 '.php' 代替。

所以新的MY_Router.php:

<?php

/*
 * Custom router function v 0.3
 *
 * Add functionality : read into more than one sub-folder
 * Compatible with Codeigniter 3.x
 *
 */

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

    function _validate_request($segments)
    {

       if (file_exists(APPPATH.'controllers/'.$segments[0].".php"))
        {
            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]);
            if (substr($this->directory, -1, 1) == '/')
                $this->directory = $this->directory . $segments[0];
            else
                $this->directory = $this->directory . '/' . $segments[0];

            $segments = array_slice($segments, 1);
            }

            if (substr($this->directory, -1, 1) != '/')
                $this->directory = $this->directory . '/';

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

            if (count($segments) > 0)
            {

                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().'/'.$segments[0].".php"))
                {
                    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.".php"))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        show_404($segments[0]);
    }
}
于 2016-10-30T15:53:37.763 回答
1

我遇到了4-5 levelsof sub-directories(like /controllers/folder1/folder2/folder3/folder4/my-controller) 的问题,并从

while(count($segments) > 0 && 
     // checks only $this->directory having a /
     is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))

while(count($segments) > 0 && 
   // check $this->directory having a /
  (is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]) ||  
      // check $this->directory not having /
      is_dir(APPPATH.'controllers/'.$this->directory.'/'.$segments[0])))

这个对我有用。

上面的一个可以,2-3 sub-directories但不适用于4-5 sub-directory层次结构。

于 2014-02-27T04:47:29.147 回答