在CodeIgniter 2.x中的数据库驱动路由 [英] Database driven routing in CodeIgniter 2.x

查看:108
本文介绍了在CodeIgniter 2.x中的数据库驱动路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个网站,将有很多路由,需要添加到路径文件(800+) - 显然我不想手动添加这些一个一个到路径配置文件。

I am creating a site that will have a lot of routes that will need to be added to the routes file (800+) - obviously I do not want to manually add these one by one into the routes config file.

任何人都可以建议从数据库中自动加载路由的最佳实践。是否有合适的库/助手将执行这个任务与版本2.x工作

Can anyone suggest the best practice to have my routes loaded automatically from the database. Is there a suitable library/helper that will perform this task that works with version 2.x

例如..

$route['apple'] = 'brands/index';
$route['blackberry'] = 'brands/index';
$route['htc'] = 'brands/index';
$route['samsung'] = 'brands/index';

这些只是我将添加到路线的几个品牌 - 这些,所以我想要从数据库加载,而不是手动键入这些。这个方法对网站性能有影响,他们从数据库加载吗?

These are just a few of the brands i'd be adding to the routes - but there are hundreds of these so i'd want to have this loaded from a database rather than manually type these in. Also - would this method have any impact on site performance having them load from the database?

我使用Codeigniter v2.1.3

I am using Codeigniter v2.1.3

推荐答案

如果你不断地查询数据库通过在 application / config / routes.php 文件中的DB调用,我想象会对性能有很大的影响。

If you were to constantly query the database (on each page load) via a DB call in the application/config/routes.php file then I imagine there would be a large impact on performance.

我建议(和我以前做的方式)是在你的 routes.php 文件的底部包括以下行,并保存所有路由到缓存文件夹中的 routes.php 文件。

What I would suggest (and the way I have done this previously) is to include the following line at the bottom of your routes.php file and save all your routes to a routes.php file in the cache folder.

require_once APPPATH . 'cache/routes.php';

然后,您可以将所有结果写入缓存文件(使用CI的文件帮助程序)

You can then write all your results to the cache file (using CI's file helper) with a function similar to the below:

public function save_routes() {
        $routes = $this->routes_model->get_all_routes();

        $data = array();

        if (!empty($routes )) {
            $data[] = '<?php if ( ! defined(\'BASEPATH\')) exit(\'No direct script access allowed\');';

            foreach ($routes as $route) {
                $data[] = '$route[\'' . $route['uri'] . '\'] = \'' . $route['controller'] . '/' . $route['action'] . '\';';
            }
            $output = implode("\n", $data);

            write_file(APPPATH . 'cache/routes.php', $output);
        }
    }

如果您在管理区域中添加新路由,

If you are adding new routes in an admin area, just run the above function each time you submit your new route and it will re-generate your routes cache file.

此方法会将您的路由保存在中,应用程序/ config / routes.php 完整,但允许您写入新的路由到缓存文件,而不会严重影响网站的性能。

This method keeps your routes in application/config/routes.php intact, but allows you to write new routes to the cache file, without drastically affecting the performance of the site.

希望有帮助!!

这篇关于在CodeIgniter 2.x中的数据库驱动路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆