Welcome Guest, Not a member yet? Register   Sign In
Beginner MVC Question
#1

[eluser]bennyhill[/eluser]
I am trying to make a catalog website with three levels, department, category, and product. I currently have one controller (catalog). I ran into a problem when I had this in the uri:

index.php/catalog/department/1/category/3

Now I realize that the problem is probably that I am calling 2 functions from the uri and each function calls a view. How do I navigate down to the product level while still being able to keep track of what department and category are selected? Do I have to set up routes in the routes.php file? In additon, does anyone know of a MVC product catalog tutorial?

Thank you.
#2

[eluser]xwero[/eluser]
You could make index function that takes three parameters which gives you urls like catalog/1/2/3 or more descriptive catalog/food/fruit/apple.
Code:
function index($department=FALSE,$category=FALSE,$product=FALSE)
{
   if($this->catalog_model->is_product($department,$category,$product))
   {
      // show product page
   }
   elseif($this->catalog_model->is_category($department,$category))
   {
     // show category page
   }
   elseif($this->catalog_model->is_department($department))
   {
     // show departement page
   }
   else
   {
     // show catalog page
   }
}
The addition of the other parameters in the higher level checks is to prevent someone from entering an url like catalog/nonfood/cars/apple. Having duplicate content on you site lowers the pagerank.

But you can keep the category, product functions as shortcuts. For instance if someone want to look up a product but doesn't know the department or category they can enter catalog/product/apple and if the product exists you can redirect the to the correct page.
#3

[eluser]bennyhill[/eluser]
If you are explicitly setting the index arguments to FALSE, how would they every return true? What would the is_product() function look like in the model?
#4

[eluser]gtech[/eluser]
In xweros example the parameters are defaulted to FALSE... if you are passing in a value it will use that, and if not it will default to FALSE
#5

[eluser]xwero[/eluser]
Code:
function is_product($department,$category,$product)
{
   if($department === FALSE OR $category === FALSE OR $product === FALSE)
   {
       return FALSE;
   }

   $query = $this->db->getwhere('catalog, array('department'=>$department,'category'=>$category,'product'=>$product));
   return ($query->num_rows() > 0)?TRUE:FALSE;
}
#6

[eluser]Deadly[/eluser]
This comes close to solving an issue I've been dealing with, both in use of ExpressionEngine and CI. Is anyone trying to handle a variable depth of segments? For example, if my URI looks like:
Code:
/country/city/category/subcategory/subcategory2/entrydetail
how does one accommodate for that, without creating an unwieldy cluster of conditionals to match against all of the various combinations?

Edit: more context: the URIs below could be equally possible, given the entry's attributes:
Code:
/country/city/category/entrydetail
or
Code:
/city/category/entrydetail
or
Code:
/country/city/category/subcategory/entrydetail
#7

[eluser]xwero[/eluser]
There needs to be some logic in the urls. If like in your examples more urls have country as the first segment. The url with city as first segment shouldn't exist. If you don't want to use an if else structure you could set up routes based on the segment count
Code:
$route['country/(.+?)/(.+?)/(.+?)/(.+?)'] = 'country/fourparams/$1/$2/$3/$4';
$route['country/(.+?)/(.+?)/(.+?)'] = 'country/threeparams/$1/$2/$3';
$route['country/(.+?)/(.+?)'] = 'country/twoparams/$1/$2';
$route['country/(.+?)'] = 'country/oneparam/$1';
This makes the if else structure smaller.
#8

[eluser]Deadly[/eluser]
Hey, thanks for such a quick response! I will give this a go. Have been debating moving to CI for a few reasons from EE, and the constraints around URIs are a big part of the reason.

So, my understanding here is that by adding these routes to the config I'm redirecting calls to the classes/functions on the right. I guess I still need to do the conditional around these though, right? And can those conditionals go in the routes file?
#9

[eluser]xwero[/eluser]
The config/routes.php is a static file. In order to check the routes you would need a database connection and at the time the route is processed you can't use the CI database class yet. You could do a similar thing in your index function.
Code:
function index()
{
   $segment_count = $this->uri->total_segments();
   switch($segment_count)
   {
     case 2: /* redirect to general country page */ ; break;
     case 3: case 4: case 5: case 6 :
       $params = str_replace('/country','',$this->uri->uri_string());
       redirect('country/param'.$segment_count.$params);
       break;
     case default : /* redirect to error page for example */ ; break;
   }
}
So basically you are using the index function as a router for that class. You could use a variant of this and do the checks to validate the url per segment count. I think there is a performance downside doing routing like this but i'm not sure.




Theme © iAndrew 2016 - Forum software by © MyBB