How to pass variable in url in codeigniter?

Member

by lew , in category: PHP Frameworks , 9 months ago

How to pass variable in url in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lily , 9 months ago

@lew 

In CodeIgniter, you can pass variables in URL by using the uri->segment() method.


Here's an example of how you can pass variables in URL in CodeIgniter:

  1. Define a route in routes.php file:
1
$route['controller/method/(:any)'] = 'controller/method/$1';


  1. In your controller, you can retrieve the variable passed in the URL like this:
1
2
3
public function method($variable) {
    echo $variable;
}


  1. Now, you can access the URL with the variable like this:
1
http://example.com/controller/method/variable


In this example, "variable" is the variable that you are passing in the URL. You can pass multiple variables in the URL and access them in the controller using the uri->segment() method.