drnrxv9383 2014-06-06 16:30
浏览 39
已采纳

CodeIgniter构建具有不同页面类型的CMS

I've just started to get into MVC with PHP and have had a good mess about with the likes of CodeIgniter and CakePHP. I'm interested to find out what people's approaches to the following would be:

Normally when I have built a website with a CMS in the past I have approached it by having a core URI table in my database. Each URI is unique and represents a page on my website (e.g. www.example.com/about would reference a record in my URI table with 'about' as the unique URI). The URI table also contains a 'type' column which tells the system what type of page it is (e.g. splash, basic, gallery or whatever). Each 'type' has a corresponding table in my database with all the data for records of that type in them (e.g. I would have tables: basic, gallery and splash). The type also tells the system which template/pagehandler to load which in turn does what it needs to do for each page type.

So if you go to www.example.com/about, my system looks in my URI table for a record with URI 'about', finds it's type to be 'basic' so it loads the basic template/pagehandler which uses the basic table in my database to load and render the page. In the CMS I follow a similar approach, I'll have add/edit forms for all of the different types in my page manager.

I was wondering how you would approach this using an MVC framework such as CodeIgniter? I essentially want to have different controllers for each type of page on both the front and back. However, when someone get's to my site, they will end up on a URI with a single level so I need to check the type of the page and pass off to the correct controller. Is there a way you would recommend of checking the type of each page and then loading the relevant controller to do the rest of the work?

  • 写回答

2条回答 默认 最新

  • duancui19840401 2014-06-08 20:22
    关注

    My approach eventually was to extend the _parse_routes() method of the Router class to check the database for any records matching the current uri and set the request with the corresponding value from the database.

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class MY_Router extends CI_Router {
    
        function __construct() {
            parent::__construct();
        }
    
        function _parse_routes() {
            require_once( BASEPATH .'database/DB'. EXT );
            $db =& DB();
    
            $routes_table_exists = $db->query("SHOW TABLES LIKE 'routes';");
            if ($routes_table_exists->num_rows > 0) {
                $uri_routes = $db->get_where('routes', array('uri' => $this->uri->uri_string()));
    
                if ($uri_routes->num_rows > 0) {
                    $row = $uri_routes->result()[0];
                    if (isset($row->request)) {
                        return $this->_set_request(explode('/', $row->request));
                    }
                }
            }
    
            parent::_parse_routes();
        }
    
    }
    

    Whether or not it's the best approach to take it seems to work so far.

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

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应