CodeIgniter Forums
dynamic controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: dynamic controller? (/showthread.php?tid=16286)



dynamic controller? - El Forum - 03-02-2009

[eluser]jdfwarrior[/eluser]
Wasn't 100% sure how to name this, and I'm not asking for a 100% explanation or a "tell me how to do this", I'm just looking for theoretical knowledge, because I'm not implementing this, just wanna know if it could work.

For a previous site I had created, I allowed the administrative users to be able to "Create" pages for the site. It would basically display fields for page name, title, etc, and an fckeditor to allow them to create content and so on. It would take all this info, dump it to the database, and make use of the _remap function in a specified controller (pages). The controller would check to see if the next segment in the url was something stored in the database, if so, retrieve the info, display it to the user. All easy stuff.

Where my question comes in is, the urls for the project were in the form of www.domain.com/pages/page_name. Would there be a way in CI to remove the controller name portion from the url by creating a pre_controller or pre_system hook to perform the database lookup to determine if this were a dynamic page and load a view and such from there? I'm not sure because I have never tried this, but from documentation, it would seem that pre_controller would be bad unless I could prevent an error from it trying to find a controller with that name and therefore throwing an error. Also, pre_system is so early on that only benchmark and hooks classes have been loaded, so would attempting to load views and such actually work at this point?

As I said I'm not actually implementing this right now, so don't feel the need to write code examples or anything. I'm just looking for a little more info on hooks. And maybe what are some practical applications of a hook?


dynamic controller? - El Forum - 03-02-2009

[eluser]pistolPete[/eluser]
Quote:Would there be a way in CI to remove the controller name portion from the url ...

I would use routing:
Code:
// first route
$route['default_controller'] = 'pages';
// put special routes here
(...)
// last route
$route[':any'] = 'pages';



dynamic controller? - El Forum - 03-02-2009

[eluser]jdfwarrior[/eluser]
Yeah I was wondering if routing would do it as well, the only thing that caught my attention with routing though would be that, if you create a custom route, and tell it which controller to load, does that perform somewhat of a redirect? will that alter the url in the process?


dynamic controller? - El Forum - 03-02-2009

[eluser]jdfwarrior[/eluser]
I think I know the answer to the first question, didn't fully think it through before I posted.

But, (wow these seem like stupid, simple questions, but they just arent mentioned in the documentation), if I use the :any selector to create the a route for all other items like that, would I have to create static routes for all other controllers that are there? Say if I have a Admin controller, would I have to create a route for it, or would it still catch it and the :any would only apply to any route thats not already there?


dynamic controller? - El Forum - 03-02-2009

[eluser]pistolPete[/eluser]
[quote author="jdfwarrior" date="1236035894"]...if I use the :any selector to create the a route for all other items like that, would I have to create static routes for all other controllers that are there?[/quote]

Yes, you have to create a route for every controller.
There is a helper function which adds the routes for you:
http://ellislab.com/forums/viewreply/261261/


dynamic controller? - El Forum - 03-02-2009

[eluser]jdfwarrior[/eluser]
Nice. Thanks for the clarification.


dynamic controller? - El Forum - 06-05-2011

[eluser]RJ[/eluser]
@pistolpete your like doesn't appear to work


dynamic controller? - El Forum - 06-05-2011

[eluser]osci[/eluser]
Yes, it should be
Code:
$route['(:any)'] = 'pages';



dynamic controller? - El Forum - 07-08-2011

[eluser]Amitabh Roy[/eluser]
@osci I guess he means the forum link posted by @pistolPete

http://ellislab.com/forums/viewreply/261261/

doesn't work