Welcome Guest, Not a member yet? Register   Sign In
Dynamic Routes
#1

[eluser]darrentaytay[/eluser]
Hi guys, I'm working on a simple Content Management System and was just wondering what the best way to handle routing of new pages created via the CMS would be?

I want my CMS to support pages having children, so an example would be;

the-team/office-team/fred-jones

I'm just not entirely sure how I can tell Codeigniter where that page is routed should the user add it themselves via the CMS.

Obviously if it was static I can just add a route but in this case it's a bit more difficult. Any guidelines/advice would be much appreciated.
#2

[eluser]AoiKage[/eluser]
I'm not sure if I understood your post correctly or not, but if the problem is just declaring dynamic routes you can actually add them using both wildcards and regular expressions.

Something like:
Code:
$route['something/(:any)/(:num)'] = 'home/something/$1/$2';
#3

[eluser]JoostV[/eluser]
If you want to set routing for your pages based on slugs you could set the default controller to be the controller that handles your CMS pages and add custom routes for all other stuff.

Code:
// If there is no match for the other routes then this must be a CMS page
$route['default_controller'] = 'page';

// Custom routes for anything that is not a CMS page slug
$route['admin/(:any)'] = "admin/$1";
$route['blog/(:any)'] = "blog/$1";
// Etc...
#4

[eluser]darrentaytay[/eluser]
I'm thinking if the User in the CMS adds a page called "about-us"

I COULD hardcode it as a route:

Code:
$route['about-us'] = "pages/index/about-us";

But obviously that's terrible.

And then if they add more Child pages and end up with a structure such as:

About Us
- The Team
-- John
-- Amy
-- Frank
--- Franks Involvement

How would I route it if someone were to go to about-us/the-team/frank?
#5

[eluser]AoiKage[/eluser]
Is there a limit in the number of child pages for every page?
#6

[eluser]darrentaytay[/eluser]
Well, no but I can imagine it going deeper than 3/4 levels for my clients.
#7

[eluser]JoostV[/eluser]
Store your pages in an mptt table and you can use any slug to retrieve the page, no matter how deep.

mptt is modified tree order traversal. It's a bitch to get it right, but once you do it's heaven on earth. I use it for nested pages, categories, etc.

It enables me to retrieve a nested tree of pages of unlimited depth in just two queries.
Also, retrieving a page with a slug like about-us/the-team/frank is not a problem.
#8

[eluser]InsiteFX[/eluser]
Create a Page Controller!

Then create a route for your Page Controller.

InsiteFX
#9

[eluser]Boris Strahija[/eluser]
This is how I do it in my content system:
Code:
$route['default_controller']     = 'front';

// Admin routes
$route[BACKEND.'/login']                = 'auth/auth_admin/login';
$route[BACKEND.'/logout']               = 'auth/auth_admin/logout';
$route[BACKEND.'/([a-zA-Z_-]+)/(:any)'] = '$1/$1_admin/$2';
$route[BACKEND.'/([a-zA-Z_-]+)']        = '$1/$1_admin/index';
$route[BACKEND]                         = 'dashboard/dashboard_admin';

$route['404_override']             = 'front';

The front controller handles all the public requests, and just compare the slugs to the content entries in your DB.




Theme © iAndrew 2016 - Forum software by © MyBB