Codeigniter动态路由 [英] Codeigniter Dynamic Routing

查看:120
本文介绍了Codeigniter动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不会做类似的事情。

http:// example.com/-主控制器

http:// example.com/ - Main Controller

http:// example.com/rules/-主控制器,内容从数据库获取,但如果不存在,则
返回404页。 (没关系,没问题。)

http:// example.com/rules/ - Main Controller where content get from database, but if not exist return 404 page. (It's ok, isn't problem.)

但是如果我在application / controlles / rules /
中有子文件夹,我想将其重定向到Main Contorller规则文件夹。

But if i have subfolder in application/controlles/rules/ I want to redirect it to Main Contorller at Rules folder.

下面的代码可以解决问题,但是我不知道它是如何实现的。
在routes.php:

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

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):
因此,似乎就像我搜索的内容(工作示例):


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 /...

http:// example.com/1/2/3/4/5/6/...


  1. 文件夹控制器具有子文件夹 1?

  2. 是:文件夹 1具有子文件夹 2?

  3. 否:文件夹 1具有控制器 2.php?

  4. 否:控制器 controllers / 1 / main.php具有功能 2?

  5. 是:重定向至http:// example.com/1/2/-其中3,4,5-参数..

  1. Folder "controllers" has subfolder "1"?
  2. YES: Folder "1" has subfolder "2"?
  3. NO: Folder "1" has controller "2.php"?
  4. NO: Controller "controllers/1/main.php" has function "2"?
  5. 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

我发现 http://codeigniter.com/forums/viewthread/97024/#490613

推荐答案

我没有仔细阅读您的问题,但这立即引起了我的注意:

I didn't thoroughly read your question, but this immediately caught my attention:

if (is_dir($path . '/' . $folder)) {
    echo "$route['$folder/(:any)'] = '$folder/index/$1';"; //<---- why echo ???
}

老实说,我不确定为什么这不会给您带来严重的问题除了不工作。

Honestly I'm not sure why this didn't cause serious issues for you in addition to not working.

您不想在此处路由 echo ,只会尝试打印字符串进行屏蔽,这种方式甚至都没有将其解释为PHP。还有一些引号需要解决的问题,以便可以将变量读取为变量,而不是字符串。改为尝试以下操作:

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:

if (is_dir($path . '/' . $folder)) {
    $route[$folder.'/(:any)'] = $folder.'/index/$1';
}

在旁边:我想提供一些并非与您的问题直接相关的其他资源,但是仍然可以帮助您解决问题:

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:

  • Preferred way to remap calls to controllers: http://codeigniter.com/user_guide/general/controllers.html#remapping
  • Easier way to scan directories: http://php.net/manual/en/function.glob.php

这篇关于Codeigniter动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆