Merijn van Essen
Merijn van Essen

Reputation: 1

dynamic routing code igniter

I build a multisite-cms which is flexible in routing, for three levels deep. So people should be able to make documents as products, pages, blogs. There are also categories but I think for in the routing it's okay to show only the first category of the product,page,blog etc

The controller is catching the page-slug and is managing the rest Although everything is working fine, I was wondering if there would be better options. I've seen some sollutions with storing slugs into the DB, and catch them in routes. But I don't believe this is necessary?

// awesome
// product/awesome
// product/category/awesome


//controller site/site/page
public function page($slug1 = NULL,$slug2 = NULL,$slug3 = NULL)
{   

   if($slug2!=NULL&&$slug3!=NULL){
      $slug = $slug3;
    }else if($slug2!=NULL&&$slug3==NULL){
      $slug = $slug2;
    }else{
      $slug = $slug1; 
   }
   // find slug and display content
 }

// routes.php
// one level 
$route['(:any)'] = 'site/site/page/$1';
// two levels deep
$route['(:any)/(:any)'] = 'site/site/page/$1/$2';
// max of three levels deep
$route['(:any)/(:any)/(:any)'] = 'site/site/page/$1/$2/$3';

Upvotes: 0

Views: 220

Answers (1)

aajiwani
aajiwani

Reputation: 121

You can abstract the variation using different methods. Like instead of having if then else, you can have 3 different methods producing different slugs as you like.

public function LoadPage($page)
{
}

public function LoadProduct($product, $page)
{
}

public function LoadBlog($blog, $product, $page)
{
}



// Your routes could refer to individuals then 

// routes.php
// one level 
$route['(:any)'] = 'site/site/LoadPage/$1';
// two levels deep
$route['(:any)/(:any)'] = 'site/site/LoadProduct/$1/$2';
// max of three levels deep
$route['(:any)/(:any)/(:any)'] = 'site/site/LoadBlog/$1/$2/$3';

Upvotes: 0

Related Questions