CodeIgniter: adding parameters to URL CodeIgniter: adding parameters to URL codeigniter codeigniter

CodeIgniter: adding parameters to URL


You can indeed use routing to do this.

$route[':any'] = "controller/method";

This will redirect EVERYTHING after your base url to the specified controller and method inside that controller. To get the url segments you can use the URI helper.

$this->load->helper('url'); // load the helper first

$city = $this->uri->segment(1);

When accessing http://localhost/codeigniter/edinburgh the $city variable in above example would be edinburgh.

Hope that helps!

PS. You don't need mod_rewrite to specify a default controller. You can set it in your config.php under Routes. Specify city as your default controller and you can get rid of the mod_rewrite.


Yes you can use a route:

$route[':any/'] = "myclass/by_city_method";

But why don't you use a module called (for instance) city to get the classical uri scheme?

class city extends Controller {    public void index($city=false) {       if ($city) { } else { }   }}

Edit: you can even choose city to be the default controller, in the global config file.


Another method:

route.php:

$route['city/(:any)'] = "city/city_lookup/$1";

city.php

<?php class City extends Controller {    function City()    {        parent::Controller();    }    function city_lookup($id)    {        echo "$id";    }}