another controller within a controller?


I have problem making my small idea come true.

I have a controller called 'user', which then takes the second segment as an user id so it's like


(Yes, I remapped it since the second segment, username, isn't a static method. If concerned, it's really /user/is_user/username)

The problem after that is I want to put another controller after this..
Say I've built controllers, models, views for these: blog, photo, guestbook, about

then I want them to come after that username in the URI. so it'd be like

http://codeigniter/index.php/user/username/blog/methods for the controller blog/

Is it possible to design like this?

I understand that I can put everything in the user controller which kinda acts like a parent controller,
but then that'd be not taking advantage of MVC pattern. No it's not even that far. That'd be a improper way to design a program. I reckon..

I just came up with this idea - how about putting all the 'user' controller functionality into a custom library,
and make that autoloaded, then just design controllers in usual way.

Is this an ideal way?

I might reroute everything though.. Tongue

You'll need HMVC (hierarchical MVC), where parent controllers call child controllers to do part of the work.

There are two solutions available: Modular Extensions (HMVC) and Modular CI. Both are documented in the Wiki.


Thanks for the reply. Seems like Modular CI is my way to go Smile..
I didn't realise the Wiki has so many pages before I clicked on 'All Pages' :o

Just bump:

After reading both Wiki on Modular CI and ME closely I thought it'd be too much for what I want.

All I wanted to have is to have the same function applied to each controller, just without having to rewrite this main 'user' controller in every single controller.
Of course, there's some URI fun involved in this matter but anyway I gave it a try.. :o

I made a library extending Controller, so its MY_Controller. I converted the 'user' controller into this library: pretty much the same except for the class name and the constructor parent:: thing..
But then I need to receive an input, which is the segment 2.

I tried to solve it using /application/config/routes.php, making something like this: $route['user/(:any)/(:any)'] = '$2';

so any url starting with "user" followed by any "username (the first (:any) = $1" will be routed to the controller, which is in the third segment.
In real world the URI should look like http://codeigniter/index.php/user/drophere/blog.
In this case, the $2 will be 'blog', routed to "blog" controller so natural way...

While doing this, the MY_Controller library, which always comes before any of the controllers as they just extend this library, will fetch some user information I desired with the second segment, $1.

This way, whatever controller comes after as long as I start my url with "user", they will be able to use the user information I was fetched in the MY_Controller library.

So I thought It'd work.. because the index page of blog controller showed up.

However I cannot use any methods on it. How sad?!

Whatever I put in the URI as method, so it is to be http://codeigniter/index.php/user/drophere/blog/METHOD, it will just show 404 page even if the blog controller DOES have the method.
I presume this is something that has to do with CI's internal routing function.. I hacked into the core and actually got the point where the 404 page is shown and tried to look up... hmm... no I'm not this good yet.
However I got this vibe: CI is trying to find the method from somewhere different.

As soon as I put another (:any) to the routes.php and trying to get to the method it actually works, but the index() is broken..........WT$#^#^@%?
I'm totally confused.


I will probably just look for the ME or the Modular CI. Even if what I want seems to fit extending the native library better, just because I got this crazy URI thing going around.. o.O

[eluser]J Maxwell[/eluser]
Your problem is that in your route you aren't defining the $3 - so that when you add the method onto the URI then it is no longer matching your route.

Try adding another route, with a third (:any) in it.


[quote author="J Maxwell" date="1283127621"]Your problem is that in your route you aren't defining the $3 - so that when you add the method onto the URI then it is no longer matching your route.

Try adding another route, with a third (:any) in it.


Hi John,

Thanks for your reply.

I realised it when I mentioned about the another (:any) line in my very last post, but when I do this it will break when the third segment is not introduced. (That means, URI without 3rd segment does not work, but instead shows 404 error)

Further more the problem is that I cannot have a pre-set amount of routing arguments in the routes.php, as the segments at the longest will vary from one controller to another.

I also realised that as soon as I reroute using routes.php it will end up there at the exact point where its routed, and won't follow the CI's default controller/method rule anymore.

Hmm.. I better study on this before I hit the Modular CI's door.

Can't you just create a parent and extend from this one? This still is MVC pattern!


Thanks for the reply. But I doubt if I really understood what you meant there..
Create what a parent? You mean make one controller parent and extend it into another controller?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

Users browsing this thread:
1 Guest(s)

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.