Codeigniter 根据 url 动态加载控制器(routes.php 之外)

发布于 2024-11-07 12:28:24 字数 561 浏览 2 评论 0原文

通常使用 CodeIgniter 必须在 config/routes.php 文件中指定控制器。 这不太方便,所以我希望能够在控制器中执行类似的操作。

  1. 获取 url 部分并检查第一部分是否在数组中指定,
  2. 如果是,则加载指定的控制器,如果没有,则加载默认控制器。

它基本上模仿了路由文件的行为,但之前不需要指定通配符。我正在使用一个基本控制器,我用每个控制器扩展,但我想让这个控制器只加载(或包含)所需的控制器。

有谁知道我怎样才能以一种好的方式做到这一点?

提前致谢。

// 编辑

好的,这是我的场景。

我有 cms,用户可以选择包含模块(例如画廊)。 我需要包含所有画廊 php 脚本,而不必在 url 中包含“画廊”。 我认为如果我使用“主控制器”,根据所选模块加载另一个控制器,它就会起作用。我意识到这可能不是最好的方法,所以如果有一种“干净”的方法,请告诉我。

据我所知,模型只是用于数据库的东西,所以把整个画廊放在那里也是不对的。插件本身当然是一个库,但是会有一些代码根据需求加载库,获取数据库数据等。

谢谢

using CodeIgniter normally one has to specify the controllers in the config/routes.php file.
This is not to handy, so I would like to be able to do something like this in a controller.

  1. get url parts and check if the first part is specified in an array
  2. if so, load the specified controller, if not, load default controller.

It basically mimics the behavior of the routes file, but there is no need to specify the wildcards before. I am using a base controller I extend with every controller, but I would like to have this controller just load (or include) the needed controller.

Does anyone have any idea how I can do this in a good way?

Thanks in advance.

// Edit

Okay, so here is my scenario.

I have cms and users can choose to include modules (e.g. a gallery).
I need to inlcude all the gallery php scripts without having to have "gallery" in the url.
I figured it would work if I use a "main controller" which loads another controller depending on the modules chosen. I realize this might not be the best way, so if there is a "clean" way to do it, please tell me.

As far as I know models are just for database stuff, so putting a whole gallery in there is not right either. The Plugin itself will of course be a library, but there is going to be some code to load the libs depending on the demands, get the database data, etc.

Thanks

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

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

发布评论

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

评论(2

殤城〤 2024-11-14 12:28:24

你这样做的方式是不正确的。您永远不应该接管路由功能来执行此操作。您需要的是使用某种模块功能来包含所需的方法和模型;模块不需要具有可路由访问的方法,因此它基本上是一个带有模型和视图的“库”。

如果我没记错的话,有几个插件可以提供此功能,其中一个是 HMVC (谷歌它)。

理想的形式是从控制器按需加载“模块”,就像使用核心 CodeIgniter 库一样;假设您在博客控制器操作中,并且想要包含在 galleryimages 上使用的 comment 模块,您只需包含该模块并调用它的方法来获取数据,然后您可以根据需要将其传递给视图;您甚至可以渲染部分并将其存储到变量中以传递给主控制器。

希望这足以让您走上正轨:)

The way you're doing this is incorrect. You never should take over the routing function to do this. What you need is to use some kind of module functionality to include the required methods and models; a module doesn't require to have route-accessible methods, so it's basically a "library" with a model and views.

If I remember correctly there are several plugins that provide this, One was HMVC (google it).

The ideal form would be to load the "Module" on demand from your controller, like you do with the core CodeIgniter Libraries; so lets say you're inside the blog controller action and you want to include the comment module which is used on gallery and on images, you just include the module and call it's methods to get the data which you can then pass to the view as needed; you can even render partials and store them into a variable to pass to your master controller.

Hope this is enough to get you on the right track :)

烟凡古楼 2024-11-14 12:28:24

我可能会误解你的问题,但如果你访问它们,你想加载你的控制器,如果没有,你想访问你的默认值。

如果我理解正确的话,你可以在你的路由中做一些事情,有一个路由将所有内容带到你的默认控制器。

在您的控制器中,有一个包含所有控制器的数组,然后将该数组内爆为正则表达式,

$array = [c1, c2, c3, c4];
$str = implode('|', $array);
$regex = "($str)"

现在只需将正则表达式添加到路由中

,然后根据您认为合适的情况进行重定向。

但这确实是路由文件的用途,您只是在围绕应该使用的东西跳舞。

I may be misunderstanding your question but you want to load your controllers if you go to them and if not you want to go to your default.

If I am understanding correctly you can do a couple things in your routes have a route that takes everything to your default controller.

In your controller have an array of all your controllers then implode the array to a regular expression

$array = [c1, c2, c3, c4];
$str = implode('|', $array);
$regex = "($str)"

now just add your regex to a route

now redirect as you see fit.

But this is really what the routes file is for you you are just dancing around something that should be used.

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