How to change a part of the url in codeigniter?

by tressie.damore , in category: PHP Frameworks , 5 months ago

How to change a part of the url in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , 5 months ago

@tressie.damore 

In CodeIgniter, you can change a part of the URL by routing the request to a specific controller and method combination.


Here are the steps to follow:

  1. Open the application/config/routes.php file.
  2. Find the $route['default_controller'] parameter. It specifies the default route for your application. Change it according to your needs if required.
  3. Below the default route, you can define custom routes. Each route consists of two parts separated by an equal sign: the URL pattern and the corresponding controller and method.


For example, if you want to change example.com/welcome to example.com/greetings, you can add the following route:

1
$route['greetings'] = 'welcome';


Here, greetings is the new URL segment, and welcome is the controller that will handle the request.


You can also specify a specific method of the controller by adding it after the controller name, separated by a forward slash. For example:

1
$route['greetings'] = 'welcome/index';


  1. Save the routes.php file.


Now, when you access example.com/greetings, CodeIgniter will route the request to the Welcome controller's index method.