路由到 CodeIgniter 中的多个子文件夹

发布于 2024-10-21 02:38:37 字数 474 浏览 2 评论 0原文

我在控制器目录中设置了一个管理文件夹,在该文件夹下有 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';

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

I have a admin folder set up in my controllers directory, under that i have 3 seperate sub-folders with controllers inside of them.

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

I tried routing it in the routes.php file like this

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

What do I do to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜无邪 2024-10-28 02:38:37

该代码已经在互联网上,但我修改了它以使其适用于 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]);
    }
}

This code was already on the internet but i modified it to make it work for codeigniter 2.1

See the old source here:
http://glennpratama.wordpress.com/2009/10/20/multi-level-subfolder-for-controller-in-codeigniter/

Make a new file MY_Router.php in the application/core directory, copy the following code in it:

<?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]);
    }
}
梦里人 2024-10-28 02:38:37

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

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

"Out of the Box" codeigniter does not support multiple subdirectory levels in your controllers directory, just one.

There is a way to extend the routing class to support this, check this blog entry.

给妤﹃绝世温柔 2024-10-28 02:38:37

对于 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]);
    }
}

For Codeigniter 3.x compatibility: The usage of the EXT constant has been deprecated since dropping support for PHP 4. There’s no longer a need to maintain different filename extensions and in this new CodeIgniter version (3.x), the EXT constant has been removed. Use just ‘.php’ instead.

So the new 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]);
    }
}
墨离汐 2024-10-28 02:38:37

我遇到了子目录4-5级问题(例如/controllers/folder1/folder2/folder3/folder4/my-controller),并将while循环从

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 子目录,但不适用于 4-5 子目录 层次结构。

I was facing problem with 4-5 levels of sub-directories(like /controllers/folder1/folder2/folder3/folder4/my-controller) and change the while loop from

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

to

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])))

It works for me.

The above one is ok for 2-3 sub-directories but not for 4-5 sub-directory hierarchy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文