我在我的控制器目录中设置了一个管理文件夹,在其下面我有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';
我该怎么做才能解决这个问题?
答案 0 :(得分: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]);
}
}
答案 1 :(得分:4)
“开箱即用”codeigniter不支持您的控制器目录中的多个子目录级别,只有一个。
There is a way to extend the routing class to support this, check this blog entry.
答案 2 :(得分: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]);
}
}
答案 3 :(得分:1)
我遇到4-5 levels
sub-directories
的问题(比如/ 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
层次结构。