Codeigniter 控制器间通信

发布于 2024-12-09 16:00:00 字数 233 浏览 0 评论 0原文

我是 MVC、CodeIginter 的新手。它需要为一个简单的应用程序编写大量代码,而不是让事情变得简单。这些可能会发生,因为我是新人。所以我对这件事没什么困惑。感谢任何形式的帮助。
1)在一个控制器中编写的方法不能在另一控制器类中访问。我必须为相同的功能编写一个新函数。

2)要在非mvc面板中创建网站管理面板(后端),我们通常在一个新文件夹中创建它。这在 CodeIgniter 中可能吗?如果不是管理员(后端)怎么办?

I am new to MVC, CodeIginter. Instead of getting things easy, it needs lot of code to be written for a simple application. These are might be happening becouse I am new. So I have few confusions about this thing. Any kind of help is appreciated.
1) Methods are written in one controller can not be accessed in another controller classes. I have to write a new function for the same functionality.

2) To create website administration panel (back-end) in none mvc panel, we usually create it in a new folder. Is this thing possible in CodeIgniter? If not what about the admin (back-end)??

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

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

发布评论

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

评论(2

不顾 2024-12-16 16:00:00

让我们尝试消除您对此的一些疑虑。

1)从另一个控制器调用一个控制器的方法是不可能的,顺便说一句,这也是有意义的。

控制器应该从 URL 获取操作(由 CI 路由到任务的正确控制器),并据此决定需要调用哪个模型以及哪个模型的方法来详细说明所请求的数据。

然后,模型将详细阐述的结果返回给控制器,而控制器又决定将该结果传递给哪个视图。

最终,视图被构造为获取这些数据并显示它们。

所以,正如你所看到的,从另一个控制器调用一个控制器的方法是无意义的,这就像进入一个页面并找到另一个页面一样;如果你想将请求传递给另一个控制器......那么,有一个重定向。

如果您发现您在某些时刻具有相同的功能,请三思:

  1. 什么是功能?您的意思是控制器“存档”中的“显示帖子”和控制器“新闻”中的“显示帖子”之类的东西吗?它们的功能几乎不一样;他们也许可以分享观点或模型,但仅此而已。
  2. 对于与 URL 无关、但涉及一些进一步阐述的函数(这在模型中可能是错误的)并且仍然在控制器中调用,您可以使用库。想想“form_validation”库,它在控制器的方法中调用,但有自己独特的(和封装的)功能。或者一个“会话”库,或一个“身份验证”库

2)要创建管理面板,最简单的事情是:创建一个“管理”控制器(然后可以访问 www.mysite.com/index.php/admin),并将所有管理操作放在其方法中:create_page()、edit_page()、manage_users() 等等。
为了避免人们自由访问它,您需要构建一个身份验证系统,在其最简单且简单的结构中,该系统可能是检查会话是否设置(可能在 __construct() 时间完成检查)。

但是您可以找到已经制作好的不错的 Auth 库,例如 Ion Auth 或 Tank Auth(据我所知,这是两个最流行的)

希望现在事情变得更清晰了。如果您对模块化 HMVC 方法感兴趣,另请参阅 Interstellar_Coder 对此答案的评论。

Let's try to clear some of your doubts about this.

1) Calling a controller's method from another controller is not possible, and it's whtouth meaning by the way.

A controller is supposed to get an action from the URL (which is routed by CI to the right controller for the task) and, based on that, decide which Model and which model's method needs be called to elaborate the data requested.

The model, then, hands back the result of this elaboration to the controller, which , in turns, decides to which view pass this results.

The view, eventually, is structured to get those datas and display them.

SO, as you can see, calling a controllers' method from another controller is nonsense, it would be like going to a page and finding another one instead; if you want to pass to another controller the request...well, there's the redirect for that.

If you find out you have the same functionalities in several moment, think twice:

  1. What is a funcionality? Do you mean somehtin like "display posts" in controller "archive" and "display posts" in controller "news" ? they're hardly the same functionality; they can maybe share views, or models, but that's it.
  2. For functions that doesn't relate to URLs, but involve some further elaboration (which might be wrong to do in Models) and are nonetheless called in a controller, you have library instead. Think at the "form_validation" library, which is called in a controller's method, but has its own peculiar (and encapsulated) functionalies. Ora a "session" library, or an "authentication" library

2) To create an admin panel the easiest thing is: create an "admin" controller (which is accesible then to www.mysite.com/index.php/admin), and put all the administration actions there, in its methods: create_page(), edit_page(), manage_users(), whatever.
In order to avoid people accessing it freely you need to build an authentication system, which, in its simplest and barabone strucutre, might be a check of wheter a session is set or not (maybe a check done at __construct() time).

But you can find nice Auth libraries out there already made, such as Ion Auth or Tank Auth (the 2 most popular to my knowledge)

Hope things are a bit clearer now. See also Interstellar_Coder's comment at this answer if you're interested in the modular HMVC approach.

就此别过 2024-12-16 16:00:00

1) 在一个控制器中编写的方法无法在另一控制器类中访问。我必须为相同的功能编写一个新函数。

功能是关于什么的?也许您应该编写一个库/帮助程序,控制器的逻辑应仅限于请求流或其他内容,但不要太复杂。为此,请将功能放入模型中,或者如果更一般的话,将功能放入库/帮助程序中。

2)要在非mvc面板中创建网站管理面板(后端),我们通常在一个新文件夹中创建它。这在 CodeIgniter 中可能吗?如果不是,那么管理员(后端)呢??

没明白,能再详细点吗?

1) Methods are written in one controller can not be accessed in another controller classes. I have to write a new function for the same functionality.

What's the functionality about? Perhaps you should write a library/helper instead, controller's logic should be limited to request flow or something else but not too complicated. For that, put the functionality in the model, or if more general, in library/helper.

2) To create website administration panel (back-end) in none mvc panel, we usually create it in a new folder. Is this thing possible in CodeIgniter? If not what about the admin (back-end)??

I don't get it, could you elaborate more?

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