Codeigniter - SPLessons

CodeIgniter URI Routing

Home > Lesson > Chapter 16
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter URI Routing

CodeIgniter URI Routing

shape Description

CodeIgniter URI Routing helps in mapping class/method relationship again in the given context. Custom flow can be incorporated rather using default class/method relationship. Below are URI routing pattern syntaxes of CodeIgniter: [php] {domain-name}/{class_name}/{function_name}/{parameters} or {domain-name}/{first-segment}/{second-segment}/{third-segment} [/php] In the above syntax, first segment => class name second segment => method or function name third segment => parameters User can set their own rules in CodeIgniter URI Routing context.

Setting Rules For CodeIgniter URI Routing

shape Description

CodeIgniter routing rules are defined in the file route.php present under the path directory application/config. An array known as $route gives the permission to define user own rules in URI routing. CodeIgniter setting up of own rules can be done in two ways:

Rules with WildCards

shape Description

There are two types of wildcards. They are: Note: Wildcards are aliases to the Regular Expressions. With :any being translated to [^/]+ and :num to [0-9]+, respectively. :num : It matches the segment that has only numbers. For example, [php]$route['(records/:num)'] = 'student/details/$1';[/php] In the above example code, it can be observed that "records" is first segment of the URL, and second segment :num will remap to the class "student" and the "details" method passing in the match as a variable to the function. If invoked https://www.school.com/records/1 or https://www.school.com/records/2 it will redirect to https://www.school.com/student/details/$1. :any : It matches the segment that has only characters. For example, [php]$route['(records/:any)'] = 'student/details';[/php] In the above example code, it can be observed that "records" is first segment of the URL, and second segment :any will remap to the class "student" and the "details" method passing in the match as a variable to the function. https://www.school.com/records/john or https://www.school.com/records/mike is invoked it will redirect to https://www.school.com/student/details. Note : Here records is the controller and details is controller function. Note : Do not use leading/trailing slashes.

Rules with Regular Expression

shape Description

Routes can also be redirected with CodeIgniter Regular Expression. [php]$route['records/([a-zA-Z0-9]+)'] = 'student/details';[/php] Using the above code URL can be invoked with own regular expressions in which special characters are not permitted to use. A segment with forward slash (‘/’) can also be matched which normally indicates the delimiter among many segments.

Summary

shape Key Points

  • CodeIgniter URI Routing helps in mapping class/method relationship again in the given context.
  • CodeIgniter URI Routing - $route gives the permission to define user own rules in URI routing.
  • CodeIgniter URI Routing - Wildcards and Regular Expression are the ways to set rules For CodeIgniter URI Routing.