how to define path in CodeIgniter?

Here's how you can manipulate or define custom routes in CodeIgniter:

  1. Routes Configuration: CodeIgniter allows you to define custom routes in the application/config/routes.php file. You can set up specific URL patterns to map to specific controllers/methods.

    $route['custom/path'] = 'controller/method';
    
  1. This tells CodeIgniter that when a user accesses http://example.com/custom/path, it should invoke the method method of the controller controller.

  2. Wildcard Routing: You can use wildcard routing to catch dynamic segments of the URL.

    $route['product/(:num)'] = 'catalog/product_lookup_by_id/$1';
    

     

  3. This route will capture any URL that starts with product/ followed by a numeric value and pass that value to the catalog/product_lookup_by_id method.

  4. Regular Expression Routing: For more complex routing needs, you can use regular expressions.

$route['product/([a-zA-Z0-9_-]+)'] = 'catalog/product_lookup_by_name/$1';
  1. This route will capture any URL that starts with product/ followed by an alphanumeric value or underscore and hyphen, passing it to the catalog/product_lookup_by_name method.

  2. Default Controller: You can specify a default controller to be invoked when no controller is specified in the URL.

    $route['default_controller'] = 'welcome';
    

     

  3. This tells CodeIgniter to invoke the index method of the welcome controller if no other controller/method is specified in the URL.

  4. Group Routing: You can group routes for easier management and organization.

    $route['blog'] = 'blog';
    $route['blog/(:any)'] = 'blog/$1';
    
     

    Here, all routes that start with blog/ will be directed to the blog controller.

Post your Answer