Codeigniter - best routes configuration for CMS?

后端 未结 2 905
谎友^
谎友^ 2021-02-06 17:48

I would like to create a custom CMS within Codeigniter, and I need a mechanism to route general pages to a default controller - for instance:

mydomain.com/about
         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-06 18:39

    You are in luck. I am developing a CMS myself and it took me ages to find a viable solution to this. Let me explain myself to make sure that we are on the same page here, but I am fairly certain that we area.

    Your URLS can be formatted the following ways:

    http://www.mydomain.com/about - a top level page with no category
    http://www.mydomain.com/services/maintenance - a page with a parent category
    http://www.mydomain.com/services/maintenace/server-maintenance - a page with a category and sub category.

    In my pages controller I am using the _remap function that basically captures all requests to your controllers and lets you do what you want with them.

    Here is my code, commented for your convenience:

    uri->uri_string();
            $segments = explode("/", $segments);
    
            // Remove blank segments from array
                foreach($segments as $key => $value) {
                   if($value == "" || $value == "NULL") {
                       unset($segments[$key]);
                   }
                }
    
                // Store our newly filtered array segments
                $segments = array_values($segments); 
    
                // Works out what segments we have
                switch (count($segments))
                {
                    // We have a category/subcategory/page-name
                    case 3:
                        list($cat, $subcat, $page_name) = $segments;
                    break;
    
                    // We have a category/page-name
                    case 2:
                        list($cat, $page_name) = $segments;
                        $subcat = NULL;
                    break;
    
                    // We just have a page name, no categories. So /page-name
                    default:
                        list($page_name) = $segments;
                        $cat = $subcat = NULL;
                    break;
                }
    
            if ($cat == '' && $subcat == '') {
    
                $page  = $this->mpages->fetch_page('', '', $page_name);
    
            } else if ($cat != '' && $subcat == '') {
    
                $page  = $this->mpages->fetch_page($cat, '', $page_name);
    
            } else if ($category != "" && $sub_category != "") {
    
                $page = $this->mpages->fetch_page($cat, $subcat, $page_name);
            }
    
                    // $page contains your page data, do with it what you wish.
    
    }
    ?>
    

    You of course would need to modify your page fetching model function accept 3 parameters and then pass in info depending on what page type you are viewing.

    In your application/config/routes.php file simply put what specific URL's you would like to route and at the very bottom put this:

    /* Admin routes, login routes etc here first */
    
    $route['(:any)'] = "pages"; // Redirect all requests except for ones defined above to the pages controller.
    

    Let me know if you need any more clarification or downloadable example code.

提交回复
热议问题