Welcome Guest, Not a member yet? Register   Sign In
Calling a Controller From a View (Or Another Controller)
#1

[eluser]Unknown[/eluser]
Hello,

I'm trying to implement a dynamic menu on my system using CI 1.5.4 with Rapyd 0.9.8
Here is my architecture:
Code:
application/
     |------controllers/
                 |------homepage.php
                 |------menu.php
     |------views/
              |------homepage.php
              |------header.php
              |------footer.php
              |------menu/
                       |--menu.php

On homepage.php view I load header and footer view. On header.php view I load menu/menu view.

The problem is; I want to dynamically populate menu from the db and wrote its codes on menu.php controller. But I cannot show its content, because the view is just loaded. Not the control called.

So how can I say CI to "load menu controller" from a view? Or maybe from a controller?

Thanks,
#2

[eluser]esra[/eluser]
The view fragment could have a dedicated helper and you can access the helper functions from your view. You can load the helper from the controller that calls your main view. If you need to access your database from the helper, create an instance of the CI object in the helper.

There are a couple of other method mentioned in other threads on the forums. Rick Jolly is using one approach based on the HMVC design pattern. xwero is using an approach similar to the helper approach mentioned above, but uses config files to control the insertion and stacking order of views inserted into a template. I'm working on someting different based on a Document Management library but it's not ready for release yet.
#3

[eluser]alpar[/eluser]
The controller is always called, and it loads the view, and models. You shouldn't load any models in your views if you want to stick to MVC. Try studying the MVC architecture, it is not like including raw php files.
#4

[eluser]esra[/eluser]
[quote author="alpar" date="1190429567"]The controller is always called, and it loads the view, and models. You shouldn't load any models in your views if you want to stick to MVC. Try studying the MVC architecture, it is not like including raw php files.[/quote]

Read the original post again--you missed the part about loading multiple controllers with models and views of their own within the same template. This is an area where MVC 2 fumbles.

Note that it is possible to nest MVC controllers. CI currently does not support this feature in the base framework. At least three users are working on extensions to solve this particular problem.
#5

[eluser]Hugh Fletcher[/eluser]
A dirty and probably frowned on way to do this...
Create a helper function, to use in your views, that loads the controller classes using the 'call_user_func' function. You can use output buffering, hijack ci output using the output class, or use what the controller method returns for output during the call.

I am using a similar method in a template/theme system i am working on for ci that works similar to the one in expression engine(templates based on uri, dynamic calls to the controllers from the template, ect).

Hope this helps.
#6

[eluser]esra[/eluser]
There are two popular architectures that I'm aware of that allow multiple MVC triads to be nested in hierarchies. There is PAC (Presentation-Abstraction-Control) which is almost as as old as MVC and HMVC (Hierarchial-Model-View-Controller). Some of the design pattern repositories regard HMVC as a sub-pattern of PAC, but they use different sets of design patterns to accomplish similar goals and the interaction between models and views is a bit different in the two design patterns. There are numerous implementations using different variants of those architectures. There are also a number of hybrid architectures based on PAC documented on JavaWorld, other trade journals, in white papers, etc. To my knowledge, there are no PAC implementations for PHP, but there is a HMVC implementation on tigris.org called Claw.

Some links...

http://www.thecentric.com/wiki/index.php/HMVC_Tutorial

http://www.javaworld.com/javaworld/jw-07...tml?page=1

http://www.vico.org/pages/PatronsDisseny...%20Abstra/

http://www.dossier-andreas.net/software_...e/pac.html

http://www.dotnetgeeks.com/blogs/dotnetg...11/66.aspx
#7

[eluser]Phil Sturgeon[/eluser]
Not that I could see myself wanting to do this, but here is a library to do it in the cleanest possible way.

Wick




Theme © iAndrew 2016 - Forum software by © MyBB