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 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录