User Avatar Damar Maulana

6 months ago

Routing in CI4

I don't understand the concept of routing in CI4. Please anyone explain me about this topic in simplest way possible.
User Avatar Fahri Farih Kusuma

6 months ago

Hi Damar,

Routing in CI4:
Routing in CodeIgniter 4 is like a traffic cop for your website. It directs incoming requests (URLs) to the right controller and method in your app. Imagine it as a set of rules that says, "When someone goes to this URL, show them this page." You define these rules in the `Routes.php` file. It's a smart way to organize your website's content and ensure visitors see what they're supposed to when they click links or type URLs.

Here's an example to help illustrate the concept of routing in CodeIgniter 4:

Let's say you have a CodeIgniter 4 application with a controller named `Pages` and methods called `home`, `about`, and `contact`. You want to make URLs like `/home`, `/about`, and `/contact` show the respective pages.

In your `app/Config/Routes.php` file, you can define routing rules like this:

$routes->get('home', 'Pages::home');
$routes->get('about', 'Pages::about');
$routes->get('contact', 'Pages::contact');

Now, when a visitor enters the URL `/about`, the routing system knows to send them to the `about` method inside the `Pages` controller. This way, you've directed the traffic to the right place, ensuring visitors see the content they expect based on the URL they entered.

Thanks for the question, Damar. Hope it helps! If you have any further questions about it or else, let us know! 😉