具有多个应用程序的 CodeIgniter URI 路由 (htaccess)

发布于 2024-11-13 09:48:21 字数 659 浏览 2 评论 0原文

你好 stackoverflow 社区,

我的 CodeIgniter 遇到了一些问题。 我在应用程序文件夹中使用两个应用程序。第一个是前端,第二个是后端。

在我的根目录中有两个 .php 文件。一个index.php 通向前端目录,一个backend.php 通向后端目录。

由于我使用 mod 重写来获得干净的 URL,因此存在问题。 URL 结构如下: www.domain.com/controller/action

这是我的前端应用程序中控制器的操作。

我使用 htacces 来摆脱域和控制器之间的 /index.php/ 。 为了访问我的后端应用程序,我希望我的 URL 是这样的 www.domain.com/admin/controller/action

因此我有这个重写规则:

RewriteCond %{REQUEST_URI} ^admin.*
RewriteRule ^admin/(.*)$ /backend.php?/$1 [L]

现在的问题是: CodeIgniter 假定 /backend/ 是第一个 URI 段,并希望将其错误地视为我的控制器。

我真的必须编辑 CodeIgniter 的核心才能告诉它不要使用服务器请求 URI 还是还有其他技巧?

提前致谢, 托马斯

Hello stackoverflow community,

I've got a little Problem with my CodeIgniter.
I use two applications in my app folder. The first one is the frontend, the second one the backend.

In my root directory I've two .php files. An index.php leading to the frontend directory and a backend.php leading to the backend directory.

Since I use mod rewrite in order to get clean URL's there is a problem with that.
The URL structure is the following: www.domain.com/controller/action

That's the action of the controller in my frontend Application.

I use htacces to get rid of the /index.php/ between domain and controller.
To access my backend application I want my URL to be like this www.domain.com/admin/controller/action

therefore I have this rewrite rule:

RewriteCond %{REQUEST_URI} ^admin.*
RewriteRule ^admin/(.*)$ /backend.php?/$1 [L]

Now the problem:
CodeIgniter assumes that /backend/ is the first URI segment, and wants to treat it mistakenly as my controller.

Do I really have to edit the core of CodeIgniter in order to tell it not to use the Server Request URI or is there another trick?

Thanks in advance,
Thomas

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

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

发布评论

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

评论(2

|煩躁 2024-11-20 09:48:21

这确实行不通。查看 config/config.php 和 config/settings.php 等以获取需要设置的常量...您确实需要在单独的“应用程序”目录中运行每个应用程序。还值得问问自己,它们是否需要成为“单独的”应用程序......

基本上,在你还可以的时候停止走这条路,这会导致心碎和大量可怕的代码。

This really isn't going to work. Check out config/config.php and config/settings.php etc for constants that need setting... You really need to run each application in a separate 'application' directory. It's also worth asking yourself if they even need to be 'separate' applications...

Basically, stop going down this path while you still can, it will lead to heartbreak, and reams of horrible code.

迟月 2024-11-20 09:48:21

我建议您不要在控制器中创建一个名为“admin”的文件夹,而不要使用两个应用程序文件夹,您还可以在模型、视图(甚至库)中创建文件夹)命名为“admin”,然后您可以将其从该项目中取出并移至其他项目。

这将允许您仅使用标准的 rewrite 来删除 index.php 来自 URL,domain.com/admin 将指向控制器内的管理文件夹。

您只需要确保根控制器文件夹中没有名为“admin.php”的控制器,否则您将遇到问题。

有关详细信息,请参阅:将控制器组织到子文件夹中 CI 文档。

另请注意,开箱即用的 MVC 框架可能并不总是最适合创建 CMS。通常,MVC 框架最适合快速创建具有指向特定控制器的相当静态的路由的 Web 应用程序。另一方面,CMS 倾向于完全控制网站,这会导致数据库驱动的路由,这通常消除了对典型控制器的需求,除非您有经过大量修改的路由系统。

Instead of having two application folders I would suggest having a folder within the controller called 'admin', you could also have folders within your models, views (even libraries) named 'admin' which you could then pull out of this project and move to other projects.

This would allow you to just use a standard rewrite to remove the index.php from the URL, and domain.com/admin would point to your admin folder within the controller.

You just need to be sure not to have a controller within the root controller folder named 'admin.php' or you will run into issues.

For more information take a look at: Organizing Your Controllers into Sub-folders within the CI docs.

Also note that an out of the box MVC framework might not always be best used for creating a CMS. Typically an MVC framework is best used to quickly create web applications which have fairly static routes pointing to specific controllers. CMS's on the other hand tend to give full control over the website, which leads to database driven routes which often eliminate the need for typical controllers unless you have a heavily modified routing system.

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