12

I don't know much about the routing concept in codeigniter, I want to pass many parameters to a single method as explained in this http://www.codeigniter.com/userguide2/general/controllers.html tutorial page.

In the url I have this

http://localhost/code_igniter/products/display/2/3/4

In my routes.php I have written

$route['products/display/(:any)'] = 'Products_controller/display';

What I thought is it will pass all the parameters (here 2/3/4) to the method 'display' automatically but I am getting 404 page not found error.

In general I want to achieve something like, if the URI is controller/method I want to route to someother_controller/its_method and pass the parameters if any to that method. How can I do it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Velu N
  • 123
  • 1
  • 1
  • 4
  • What version of codeigniter you are using Also new location for the codeigniter documents can be found here. http://www.codeigniter.com/docs –  Jul 29 '15 at 05:21
  • codeigniter 3.0 I want to pass those parameters to display method can you help me please! – Velu N Jul 29 '15 at 05:23
  • Have you made sure the controller file name Products_controller.php and on routes try with lower case $route['products/display/(:any)'] = 'products_controller/display/$1'; –  Jul 29 '15 at 05:25
  • You might be best to look at codeigniter 3 uri routing http://www.codeigniter.com/user_guide/general/routing.html –  Jul 29 '15 at 05:27
  • Thanks wolfgang1983 , as you said I have named the file "Products_controller" and used lower case in routes.php also the answer by Suvash worked fine ! but If I miss anyone of the parameters I am again getting 404 not found.how can I write route rule which can pass all the parameters even if I miss one of them? in such case I want to pass a default value say 0 to that parameter. how can I do it? – Velu N Jul 29 '15 at 05:33

4 Answers4

23

In CI 3.x the (:any) parameter matches only a single URI segment. So for example:

$route['method/(:any)/(:any)'] = 'controller/method/$1/$2';

will match exactly two segments and pass them appropriately. If you want to match 1 or 2 you can do this (in order):

$route['method/(:any)/(:any)'] = 'controller/method/$1/$2';
$route['method/(:any)'] = 'controller/method/$1';

You can pass multiple segments with the (.+) parameter like this:

$route['method/(.+)'] = 'controller/method/$1';

In that case the $1 will contain everything past method/. In general I think its discouraged to use this since you should know what is being passed and handle it appropriately but there are times (.+) comes in handy. For example if you don't know how many parameters are being passed this will allow you to capture all of them. Also remember, you can set default parameters in your methods like this:

public function method($param=''){}

So that if nothing is passed, you still have a valid value.

You can also pass to your index method like this:

$route['method/(:any)/(:any)'] = 'controller/method/index/$1/$2';
$route['method/(:any)'] = 'controller/method/index/$1';

Obviously these are just examples. You can also include folders and more complex routing but that should get you started.

greco.roamin
  • 799
  • 1
  • 6
  • 20
  • Thank you very much greco.roamin Your post also helped me! does the order matter in routes.php ? $route['method/(:any)/(:any)'] = 'controller/method/$1/$2'; $route['method/(:any)'] = 'controller/method/$1'; or $route['method/(:any)'] = 'controller/method/$1'; $route['method/(:any)/(:any)'] = 'controller/method/$1/$2 which one is correct? – Velu N Jul 29 '15 at 05:40
  • You're welcome. Yes, order matters. From the CI docs, "Routes will run in the order they are defined. Higher routes will always take precedence over lower ones." – greco.roamin Jul 29 '15 at 13:41
  • how can i access the variables in controller? i tried using ($id = null) { print_r($id); print_r($_GET);die(); but both didnt return any value – guradio Apr 29 '20 at 13:36
  • @guradio if you mean how can you access the $1, $2, etc. variables in the controller, you need give them a name in the passed in parameters. For example, if your route is something like someMethod/$1 then in your controller your someMethod method need a parameter to "catch" it, like this: someMethod($id). Then inside someMethod you can access $id which will be a string. – greco.roamin May 05 '20 at 19:36
  • @greco.roamin In CI 4 Why $routes->get('messages/get_just_message/(.+)', 'Messages::get_just_message/$1'); getting 404 error. I mean controller is not called. I am using this for get AJAX and Controller [public function get_just_message($to_user_id) { ... }] – Vipul Jethva May 14 '22 at 06:09
3

On codeigniter 3

Make sure your controller has first letter upper case on file name and class name

application > controllers > Products_controller.php

<?php

class Products_controller extends CI_Controller {

    public function index() {

    }

    public function display() {

    }
}

On Routes

$route['products/display'] = 'products_controller/display';
$route['products/display/(:any)'] = 'products_controller/display/$1';
$route['products/display/(:any)/(:any)'] = 'products_controller/display/$1/$2';
$route['products/display/(:any)/(:any)/(:any)'] = 'products_controller/display/$1/$2/3';

Docs For Codeigniter 3 and 2

http://www.codeigniter.com/docs

  • You all are great thanks for all your help! Wolfgang1983 thanks for your link and I thank you all again. It works! – Velu N Jul 29 '15 at 05:46
  • how can i access the variables in controller? i tried using ($id = null) { print_r($id); print_r($_GET);die(); but both didnt return any value – guradio Apr 29 '20 at 13:36
1

Maintain your routing rules like this

$route['products/display/(:any)/(:any)/(:any)'] = 'Products_controller/display/$2/$3/$4';

Please check this link Codeigniter URI Routing

Suvash sarker
  • 3,140
  • 1
  • 18
  • 21
  • Yeah thanks Suvash I did this and it works well but what if I don't have the param in URI ? insuch cases I want to pass a default value to that parameter ! say the final one($4) is missing, I am getting 404 , but if I had it then it works well. clearly how can I route even if don't know how many parameters will be there in URI? – Velu N Jul 29 '15 at 05:27
  • Then you can use regular expressions to define your routing rules. Please check this link http://www.codeigniter.com/userguide3/general/routing.html – Suvash sarker Jul 29 '15 at 05:45
1

In CodeIgniter 4

Consider Product Controller with Show Method with id as Parameter

http://www.example.com

/product/1

ROUTE Definition Should be

$routes->get("product/(:any)", "Com\Atoconn\Product::show/$1");

Vivek Pawar
  • 339
  • 3
  • 5