URL Segment Tutorial in CodeIgniter 4 | About URI Segments

Share this Article
Reading Time: 5 minutes
1,591 Views

When we create a URL, it will be either a normal url, url with parameters, url with query strings etc. Inside this article we will see about complete details of URL segment Tutorial in Codeigniter 4.

In comparison of older version, CodeIgniter 4 has a different approch to get uri segments. By the concept of URI segments, we will get host, port, segments, query parameters etc very easily. It will be very interesting to see and learn.

Learn More –

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


Working with URI Segments at Controller

Using with Controller and views, there are slight changes depends where we are using the concept of URL segments.

Let’s take an example to understand every part in details. Next, We need a route, a method in controller.

Create Route

Open Routes.php from /app/Config and add this.

$routes->get('admin/my-route', 'SampleController::sampleMethod');

Create Controller

Open project into terminal and run this spark command.

$ php spark make:controller Sample --suffix

It will create SampleController.php into /app/Controllers folder.

Open SampleController.php and write this piece of code into it.

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class SampleController extends BaseController
{
    public function sampleMethod()
    {
        echo "<h3>URL Segment From Controller</h3>";

        echo $this->request->uri; // Output: http://localhost:8080/admin/my-route
        echo "<br/>";
        echo $this->request->uri->getSegments(); // Output: Returns all segment into array format
        echo "<br/>";
        echo $this->request->uri->getSegment(1); // Output: admin
        echo "<br/>";
        echo $this->request->uri->getScheme(); // Output: http
        echo "<br/>";
        echo $this->request->uri->getHost(); // Output: localhost
        echo "<br/>";
        echo $this->request->uri->getPort(); // Output: 8080
        echo "<br/>";
        echo $this->request->uri->getPath(); // Output: admin/my-route
        echo "<br/>";

        // IF url = http://localhost:8080/admin/my-route?name=sanjay&email=sanjay@gmail.com

        echo $uri->getQuery(); // Output: name=sanjay&email=sanjay%40gmail.com
    }
}

For above code we have considered the URL as – http://localhost:8080/admin/my-route and each written method is working for this URL.

Here, We have taken the uri segment using $this->request->uri. Keep in mind we are at controller file.


Working with URI Segments at View Files

Create a view file with any name at /app/Views folder. Open view file and write this piece of code to use uri segment concept.

<?php

echo "<h3>URL Segment From View</h3>";

//$uri = current_url(true); OR service('uri');

$uri = service('uri'); // Loading 'uri' service

echo $uri; // Output: http://localhost:8080/admin/my-route
echo "<br/>";
echo $uri->getSegments(); // Output: Returns all segment into array format
echo "<br/>";
echo $uri->getSegment(2); // Output: my-route
echo "<br/>";
echo $uri->getScheme(); // Output: http
echo "<br/>";
echo $uri->getHost(); // Output: localhost
echo "<br/>";
echo $uri->getPort(); // Output: 8080
echo "<br/>";
echo $uri->getPath(); // Output: admin/my-route
echo "<br/>";

// IF url = http://localhost:8080/admin/my-route?name=sanjay&email=sanjay@gmail.com

echo $uri->getQuery();

// Output: name=sanjay&email=sanjay%40gmail.com

Here, we have loaded uri service into view file by using service(‘uri’).

We hope this article helped you to learn about URL Segment Tutorial in CodeIgniter 4 & URI Segments in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here

Leave a Comment