在CodeIgniter中声明一个全局可见的数组用于路由

发布于 2024-12-25 12:23:08 字数 1030 浏览 0 评论 0原文

(对 CodeIgniter 和 php 来说是全新的,对编程来说并不陌生)

在 CodeIgniter 中,如何或在哪里可以声明一个可在路由器 (CI_Router)、我的自定义控制器和在视图加载器(CI_Loader)中?我想避免在路由器中硬编码数据并多次指定相同的数据(在路由器和控制器和/或视图中)。

(解释如下;也许有更好的方法来完成我想做的事情。)

我有一个发布我的免费软件应用程序的网站。该网站都是静态的,但手动维护纯 html 是一场噩梦,因此我尝试使用 CodeIgniter 动态生成页面。除了一些松散的内容(例如“欢迎”或“联系信息”)之外,该网站的大部分内容都由高度结构化的应用程序部分组成。每个应用程序都有相同的一组页面:关于、下载、屏幕截图等。这些页面都具有相同的布局,仅内容不同。

我主要关心的是可维护性以及添加新应用程序(或所有应用程序的新页面)所需的工作量。我认为没有理由使用多个控制器(甚至多个视图),因为唯一的区别在于数据(应用程序标识符),而不是行为或页面布局。

我的问题是路由。我可以做类似

$route['app1'] = 'main/apps/app1';
$route['app2'] = 'main/apps/app2';

...或循环应用程序标识符数组的操作,但这看起来真的很糟糕。当我添加应用程序时,我不必修改路由器,因为它只是添加到列表中的另一个标识符,它将用于从适当的文件中选取页面内容。

我想做的是(伪代码):

foreach (app in list_of_apps) {
  foreach (app_page in list_of_pages) {
    route['app/app_page'] = 'main/index/app/app_page'
  }
}

这将处理“app1/download”或“app2/screenshots”等的所有请求。那么我在哪里可以声明数组 $list_of_apps 和 $list_of_pages 以便它们在路由器、控制器以及稍后的视图中可见?

(Totally new to CodeIgniter and php, not new to programming)

In CodeIgniter, how or where can I declare an array that will be accessible in the router (CI_Router), in my custom controller and in the view loader (CI_Loader)? I want to avoid hard-coding data in the router and specifying the same data more than once (in the router and in the controller and/or view).

(Explanation follows; perhaps there is a better way to do what I'm trying to do.)

I have a site where I publish my freeware apps. The site is all static, but maintaining plain html by hand is a nightmare, so I'm trying to use CodeIgniter to generate the pages dynamically. Apart from a few loose ends like 'welcome' or 'contact info', the bulk of the site consists of highly structured application sections. Each app has the same set of pages: about, download, screenshots, etc. These pages all have the same exact layout and only differ in contents.

My main concern is maintainability and the amount of work that required to add a new app (or a new page for all apps). I see no reason to use multiple controllers (or even multiple views), because the only difference is in data (application identifiers), not in behavior or page layout.

My problem is with routing. I could do something like

$route['app1'] = 'main/apps/app1';
$route['app2'] = 'main/apps/app2';

...or loop over an array of app identifiers, but that seems really bad. When I add an application, I should not have to modify the router, since it's just another identifier added to a list, that will be used to pick page contents from an appropriate file.

What I would like to do instead is (pseudocode):

foreach (app in list_of_apps) {
  foreach (app_page in list_of_pages) {
    route['app/app_page'] = 'main/index/app/app_page'
  }
}

This would take care of all requests for 'app1/download' or 'app2/screenshots', etc. So where can I declare the arrays $list_of_apps and $list_of_pages so that they are visible in the router, in the controller, and later in view as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕梦 2025-01-01 12:23:08

这应该是一个简单的正则表达式解决方案:

// set this to whatever controller/method you want as the index for the app
$route['([^/])']      = 'main/apps/$1';

// this is a sample "subpage" handler
$route['([^/])/(.*)'] = 'main/index/$1/$2';

如果所有“应用程序”都遵循相同的 URL 结构,那么使其变得非常模块化应该不会太难。

This should be a simple regular expressions solution:

// set this to whatever controller/method you want as the index for the app
$route['([^/])']      = 'main/apps/$1';

// this is a sample "subpage" handler
$route['([^/])/(.*)'] = 'main/index/$1/$2';

If all "apps" follow the same URL structure, it shouldn't be too hard to make this very modular.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文