Codeigniter - setting the best routes for CMS?

I would like to create a custom CMS in Codeigniter, and I need a mechanism to route shared pages to the default controller - for example:

mydomain.com/about
mydomain.com/services/maintenance

They will be routed through my controller-manipulator. By default, the routing behavior in Codeigniter, of course, is directed to the appropriate controller and method, so in the above examples, this requires the About controller and the services controller. This is obviously not a practical or even sensible approach.

I saw the following solution for hosting in routes.php:

$route['^(?!admin|products).*'] = "pagehandler/$0";

But this creates my own problems, which I believe in. For example, it just searches for “products” in the uri request and finds the routes to the Products controller - but what if we have services / products as a CMS page? Is this then directed to the product controller?

Is there an ideal approach to this? I do not want to have routing where all the content of the CMS has a controller name prefix, but I should also be able to redefine the routing for other controllers altogether.

+3
source share
2 answers

If you are using CodeIgniter 2.0 (which is stable enough to be used for several months), you can use:

$route['404_override'] = 'pages';

, , . PHP, , 404.

, , CodeIgniter 2.0. , CMS, ​​ PyroCMS, v1.0 .

+3

. CMS, , . , , , , .

URL- :

http://www.mydomain.com/about -
http://www.mydomain.com/services/maintenance -
http://www.mydomain.com/services/maintenace/server-maintenance - .

_remap, , .

, :

<?php

class Pages extends Controller {

    // Captures all calls to this controller
    public function _remap() 
    {
        // Get out URL segments
        $segments = $this->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.

}
?>

, , , 3 , , .

application/config/routes.php , URL- , :

/* Admin routes, login routes etc here first */

$route['(:any)'] = "pages"; // Redirect all requests except for ones defined above to the pages controller.

, .

+3

Source: https://habr.com/ru/post/1765057/


All Articles