Codeigniter 动态路由
嗨,我不会做那样的事情。
http:// example.com/ - 主控制器
http:// example.com/rules/ - 主控制器,内容从数据库获取,但如果不存在 返回404页面。 (没关系,不是问题。)
但是如果我在 application/controlles/rules/ 中有子文件夹 我想将其重定向到规则文件夹中的主控制器。
下面的代码可以解决问题,但我不知道它是如何正确实现的。 在routes.php:
$route['default_controller'] = "main";
$route['404_override'] = '';
$dirtest = $route['(:any)'];
if (is_dir(APPPATH.'controllers/'.$dirtest)) {
$route['(:any)'] = $dirtest.'/$1';
} else {
$route['(:any)'] = 'main/index/$1';
}
好的,我有:
controllers/main.php
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('main_model');
}
public function index($method = null)
{
if (is_dir(APPPATH.'controllers/'.$method)) {
// Need re-rout to the application/controllers/$method/
} else {
if ($query = $this->main_model->get_content($method)) {
$data['content'] = $query[0]->text;
// it shows at views/main.php
} else {
show_404($method);
}
}
$data['main_content'] = 'main';
$this->load->view('includes/template', $data);
}
}
再次更新(routes.php): 所以,看起来就像我搜索的(工作示例):
$route['default_controller'] = "main";
$route['404_override'] = '';
$subfolders = glob(APPPATH.'controllers/*', GLOB_ONLYDIR);
foreach ($subfolders as $folder) {
$folder = preg_replace('/application\/controllers\//', '', $folder);
$route[$folder] = $folder.'/main/index/';
$route[$folder.'/(:any)'] = $folder.'/main/$1';
}
$route['(:any)'] = 'main/index/$1';
但是,完全需要这样的:
http://example.com/1/2/3/4/5/6/...
- 文件夹“controllers”有子文件夹“1”?
- 是:文件夹“1”有子文件夹“2”吗?
- 否:文件夹“1”有控制器“2.php”?
- 否:控制器“controllers/1/main.php”具有功能“2”?
- 是的:重定向到 http:// example.com/1/2/ - 其中 3,4,5 - 参数..
当您具有如下结构时,这真的很好:
http://example.com/blog/ - recent blog's posts
http://example.com/blog/2007/ - recent from 2007 year blog's posts
http://example.com/blog/2007/06/ - same with month number
http://example.com/blog/2007/06/29/ - same with day number
http://example.com/blog/web-design/ - recent blog's post's about web design
http://example.com/blog/web-design/2007/ - blog' posts about web design from 2007 years.
http://example.com/blog/current-post-title/ - current post
我发现同样有趣 http://codeigniter.com/forums/viewthread/97024/#490613
Hi i wont to make something like that.
http:// example.com/ - Main Controller
http:// example.com/rules/ - Main Controller where content get from database, but if not exist
return 404 page. (It's ok, isn't problem.)
But if i have subfolder in application/controlles/rules/
I want to redirect it to Main Contorller at Rules folder.
This follow code can solve problem, but i don't know how it right realise.
At routes.php:
$route['default_controller'] = "main";
$route['404_override'] = '';
$dirtest = $route['(:any)'];
if (is_dir(APPPATH.'controllers/'.$dirtest)) {
$route['(:any)'] = $dirtest.'/$1';
} else {
$route['(:any)'] = 'main/index/$1';
}
Ok, what I have:
controllers/main.php
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('main_model');
}
public function index($method = null)
{
if (is_dir(APPPATH.'controllers/'.$method)) {
// Need re-rout to the application/controllers/$method/
} else {
if ($query = $this->main_model->get_content($method)) {
$data['content'] = $query[0]->text;
// it shows at views/main.php
} else {
show_404($method);
}
}
$data['main_content'] = 'main';
$this->load->view('includes/template', $data);
}
}
Updated Again (routes.php):
So, seem's like what i search (work example):
$route['default_controller'] = "main";
$route['404_override'] = '';
$subfolders = glob(APPPATH.'controllers/*', GLOB_ONLYDIR);
foreach ($subfolders as $folder) {
$folder = preg_replace('/application\/controllers\//', '', $folder);
$route[$folder] = $folder.'/main/index/';
$route[$folder.'/(:any)'] = $folder.'/main/$1';
}
$route['(:any)'] = 'main/index/$1';
But, in perfectly need some like this:
http:// example.com/1/2/3/4/5/6/...
- Folder "controllers" has subfolder "1"?
- YES: Folder "1" has subfolder "2"?
- NO: Folder "1" has controller "2.php"?
- NO: Controller "controllers/1/main.php" has function "2"?
- YES: redirect to http:// example.com/1/2/ - where 3,4,5 - parameters..
It is realy nice, when you have structure like:
http://example.com/blog/ - recent blog's posts
http://example.com/blog/2007/ - recent from 2007 year blog's posts
http://example.com/blog/2007/06/ - same with month number
http://example.com/blog/2007/06/29/ - same with day number
http://example.com/blog/web-design/ - recent blog's post's about web design
http://example.com/blog/web-design/2007/ - blog' posts about web design from 2007 years.
http://example.com/blog/current-post-title/ - current post
Same interesting i find http://codeigniter.com/forums/viewthread/97024/#490613
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有彻底阅读你的问题,但这立即引起了我的注意:
老实说,我不确定为什么这除了不工作之外没有给你带来严重的问题。
您不想在此处
echo
路由,它只会尝试将字符串打印到屏幕上,这样它甚至不会被解释为PHP。引号还存在一些问题需要修复,以便变量可以被读取为变量,而不是字符串。试试这个:旁白:我想提供一些与您的问题不直接相关的额外资源,但应该可以帮助您找到解决方案:
I didn't thoroughly read your question, but this immediately caught my attention:
Honestly I'm not sure why this didn't cause serious issues for you in addition to not working.
You don't want to
echo
the route here, that will just try to print the string to screen, it's not even interpreted as PHP this way. There are also some issues with quotes that need to be remedied so the variables can be read as variables, not strings. Try this instead:Aside: I'd like to offer some additional resources that are not directly related to your problem, but should help you nonetheless with a solution:
很难说为什么你的路由注册失败。从您的代码中我可以看到您没有注册路由(您只是回显它们),此外我还发现字符串中变量的使用不一致。可能你混合了一点,关于路由的 codeigniter 文档对此也不精确(在一些小问题上,他们的代码示例在语法上并不真正正确,但总的来说它很好)。
我建议您首先将注册动态路由的逻辑移动到它自己的函数中。这将使事情更加模块化,您可以更轻松地更改事情,并且不会用变量污染全局名称空间。
基于此,我对您的代码进行了一些重构。这并不意味着这有效(未经测试),但是当您阅读它时,它可能会让事情变得更清楚:
接下来您可能不想将所有内容转移到
index
操作上,而是将 <改为代码>动态操作。此外,您可能希望有一个基本控制器,它将所有内容委托给子控制器,而不是为每个控制器添加路由。但这取决于你。上面的示例基于您在问题中概述的目录方法。编辑:其他信息可在旁边的控制器部分中找到URI 路由部分
It's hard to say why the registering of your routes fails. From your code I can see that you're not registering the routes (you just echo them), additionally I see that the usage of variables in strings are used inconsistently. Probably you mixed this a bit, the codeigniter documentation about routes is not precise on it either (in some minor points, their code examples are not really syntactically correct, but overall it's good).
I suggest you first move the logic to register your dynamic routes into a function of it's own. This will keep things a bit more modular and you can more easily change things and you don't pollute the global namespace with variables.
Based on this, I've refactored your code a bit. It does not mean that this works (not tested), however it might make things more clear when you read it:
Next to this you probably might not want to shift everything onto the
index
action, but adynamic
action instead. Furthermore, you might want to have a base controller that is delegating everything into the sub-controllers instead of adding the routes per controller. But that's up to you. The example above is based on the directory approach you outlined in your question.Edit: Additional information is available in the Controllers section next to the URI Routing section
这一切看起来有点复杂。
另外,如果数据库中有数百(或数千或更多?)可能的路线,您可能不希望每次在应用程序中加载任何页面时都将它们全部加载到“$routes”数组中。
相反,为什么不这样做呢?
routes.php 的最后一行:
控制器文件:Vanity.php:
All this seems kind of complicated.
Plus, if you have hundreds (or thousands or more?) of possible routes in a database you may not want to load all of them into the "$routes" array every time any page loads in your application.
Instead, why not just do this?
last line of routes.php:
Controller file: Vanity.php: