URL Segment Tutorial in CodeIgniter 4 | About URI Segments

Reading Time: 4 minutes
15,885 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 –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

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

Either we can do via renaming file as simple as that. Also we can do by terminal command.

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.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# 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.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more