Codeigniter 4 – URL’s with Examples

URL Basics

- URLs in CodeIgniter 4 are handled by the URI class.
- The URI class provides methods for working with URLs.

URL Structure

- CodeIgniter 4 URLs follow the following structure: `(link unavailable)
- controller is the name of the controller class.
- method is the name of the method in the controller class.
- parameter is an optional parameter passed to the method.

Routing

- Routing in CodeIgniter 4 allows you to map URLs to specific controllers and methods.
- Routes are defined in the app/Config/Routes.php file.

Example: app/Config/Routes.php

<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

class Routes extends BaseConfig
{
    public $routes = [
        'users' => 'UserController::index',
        'users/create' => 'UserController::create',
        'users/(:segment)' => 'UserController::edit/$1',
    ];
}

URL Helpers

- CodeIgniter 4 provides several URL helpers for working with URLs.
- The site_url() helper generates a URL to a controller method.
- The base_url() helper generates the base URL of the application.
- The current_url() helper generates the current URL.

Examples:

echo site_url('UserController::index'); // Output: (link unavailable)
echo base_url(); // Output: (link unavailable)
echo current_url(); // Output: (link unavailable)

URL Segments

- URL segments are the parts of the URL separated by slashes.
- CodeIgniter 4 provides methods for working with URL segments.

Example:

$segments = $this->uri->getSegments();
echo $segments[0]; // Output: users
echo $segments[1]; // Output: edit
echo $segments[2]; // Output: 1

Query Strings

- Query strings are the part of the URL after the ? character.
- CodeIgniter 4 provides methods for working with query strings.

Example:

$queryString = $this->uri->getQueryString();
echo $queryString; // Output: foo=bar&baz=qux

I hope this helps! Let me know if you have any further questions or need more information.

Leave a Reply

Shopping cart0
There are no products in the cart!
Continue shopping
0