CodeIgniter Forums
URI like controller/var/controller/var ? - 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: URI like controller/var/controller/var ? (/showthread.php?tid=60945)



URI like controller/var/controller/var ? - El Forum - 08-06-2014

[eluser]Unknown[/eluser]
my system:

models:
category
brand
vehicle

controllers:
home
brand
category
vehicle
etc.

DB:

brand
id | name
category
id | name (enum car, truck, bike etc)
vehicle
id | model | id_brand | id_category

In the 'home' controller, the 'index' method list all brands. There is a link on each brand to 'brand/view/name'

The method 'view' of 'brand' controller shows brand's name and a list like this:

CAR | TRUCK | ANOTHERCATEGORY
total: 55 | total: 21 | total: 0

In a system like this, which is the best way to link each of these itens of the list to the 'vehicle' controller?
In regular php i would do like this:
vehicle.php?id_brand=x&id_category=y

I made it work like this on CI:
$route['brand/category/(:any)']='category/view/$1';
brand/category/honda/car

that's ok, but would be more intuitive if i could make URI look like:
brand/honda/category/car

and for vehicle controller:
brand/honda/category/car/vehicle/civic

i think i can create one controller(and turn it my default controller) and make a method for remap this URI, but is there any better option?

ps: sorry for bad english, its not my native language.


URI like controller/var/controller/var ? - El Forum - 08-06-2014

[eluser]CroNiX[/eluser]
I think even more intuitive would be /cars/honda/civic
You could even do /cars/honda/civic/2012 to include the model year

If you only want one controller, instead of having to have a "cars" controller, you'd just

Code:
$route['cars/(:any)/(:any)'] = 'brand/view/$1/$2';
$route['cars/(:any)'] = 'brand/view/$1/';
$route['cars'] = 'brand/view';

And the brand controller would look something like
Code:
class Brand extends CI_Controller {

  public function view($make = '', $model = '')
  {
    if ( ! empty($make))
    {
       //They entered a car Make
    }
    if ( ! empty($model))
    {
       //They entered a car Model
    }
  }
}

This would allow for urls like:
/cars (list all car makes)
/cars/honda (list all hondas)
/cars/honda/civic (list all honda civics)

There are many ways to do this, but hopefully this gives you an idea.


URI like controller/var/controller/var ? - El Forum - 08-07-2014

[eluser]Unknown[/eluser]
got it.
ty a lot!