Codeigniter 控制器间通信
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们尝试消除您对此的一些疑虑。
1)从另一个控制器调用一个控制器的方法是不可能的,顺便说一句,这也是有意义的。
控制器应该从 URL 获取操作(由 CI 路由到任务的正确控制器),并据此决定需要调用哪个模型以及哪个模型的方法来详细说明所请求的数据。
然后,模型将详细阐述的结果返回给控制器,而控制器又决定将该结果传递给哪个视图。
最终,视图被构造为获取这些数据并显示它们。
所以,正如你所看到的,从另一个控制器调用一个控制器的方法是无意义的,这就像进入一个页面并找到另一个页面一样;如果你想将请求传递给另一个控制器......那么,有一个重定向。
如果您发现您在某些时刻具有相同的功能,请三思:
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:
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.
功能是关于什么的?也许您应该编写一个库/帮助程序,控制器的逻辑应仅限于请求流或其他内容,但不要太复杂。为此,请将功能放入模型中,或者如果更一般的话,将功能放入库/帮助程序中。
没明白,能再详细点吗?
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.
I don't get it, could you elaborate more?