CodeIgniter Forums
Routing and controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Routing and controllers (/showthread.php?tid=25886)



Routing and controllers - El Forum - 12-29-2009

[eluser]aviator2[/eluser]
Hi guys! I've got a tricky question.
How can I call controllers in other way, than through a URI?

Let's say, I would like to have a URL like this one: example.com/archive/blog/article.html
Basically, archive would be a controller, and blog is a function. But... My intention is to use a "archive/blog" for navigations and "article.html" as content identifier.
So I will probably need some router script that has to decide which controller is associated to this content and load it.

subsequent questions:
- how to solve URI
- how to implement router script
- how to call controllers

Have you got any ideas how to solve this concept?

Thank you so much!

Martin


Routing and controllers - El Forum - 12-29-2009

[eluser]Andrew Cairns[/eluser]
Hi Martin,

My first thought would be to change your planned uri.
I would have a Blog controller, as you are viewing the archive of the blog - unless you have multiple archives, then .. ignore this Smile

Your route for this would then be something like:
$route['blog/archive/(:any)'] = "blog/controller_name_here/method_you_want_to_call/$1";
(I use the HMVC extension, so blog would be my module and archive would be my controller)

You could also:
$route['blog/archive/(:any)'] = "controller_name_here/method_you_want_to_call/$1";

Viewing a current blog post would be almost identical:
$route['blog/(:any)'] = "controller_name_here/method_you_want_to_call/$1";

if using HMVC:
$route['blog/(:any)'] = "blog/controller_name_here/method_you_want_to_call/$1";


Routing and controllers - El Forum - 12-29-2009

[eluser]n0xie[/eluser]
I would just use database driven URL's.

Make a routing table. Put the url there. Put the controller you want to load there as well as the function (and maybe arguments). Extend the Router Class. Let it do it's normal checking for controllers. If it doesn't find a controller, it usually shows a 404. Override this behaviour by making it look in the database for the url. If a match is found, load the controller and function you associated with it in your database. Else show a 404.


Routing and controllers - El Forum - 12-29-2009

[eluser]aviator2[/eluser]
Andrew, Thank's for your suggestion! But my example was maybe little bit cunfusing Smile I'm not working on blog engine, but something between CMS and a corporate system. More schematic example of URL would be better: example.com/section/category/sub-category/content.html

So this section of URL "section/category/sub-category" is going to be a navigation and again, content.html is identifier.

According to your routing "hack" in routes.php, I should define each piece of navigation to this file. It looks like a "static" solution, not for dynamic navigation.

But again, thanks for post, will use it later!


Routing and controllers - El Forum - 12-29-2009

[eluser]Andrew Cairns[/eluser]
The best solution I have found for dynamic uri segments is to use a hook to generate a global array of routes and merge with the $route array.


Routing and controllers - El Forum - 12-29-2009

[eluser]Andrew Cairns[/eluser]
What "hack" are you referring to aviator2 ? those were just standard routes I posted.


Routing and controllers - El Forum - 12-29-2009

[eluser]aviator2[/eluser]
[quote author="n0xie" date="1262130507"]I would just use database driven URL's.

Make a routing table. Put the url there. Put the controller you want to load there as well as the function (and maybe arguments). Extend the Router Class. Let it do it's normal checking for controllers. If it doesn't find a controller, it usually shows a 404. Override this behaviour by making it look in the database for the url. If a match is found, load the controller and function you associated with it in your database. Else show a 404.[/quote]

Nice one, I like it!
Can you link me to some resource, where can I find inspiration how to modify a router class?


Routing and controllers - El Forum - 12-30-2009

[eluser]n0xie[/eluser]
Take a look here