dongzai3139 2011-08-07 18:19
浏览 75
已采纳

Codeigniter动态路由

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

  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

Same interesting i find http://codeigniter.com/forums/viewthread/97024/#490613

  • 写回答

3条回答 默认 最新

  • dsgffz2579 2011-08-07 23:36
    关注

    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.

    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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错