• 9

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

CodeIgniter - Optional parameters

I'm building my first CodeIgniter application and I need to make URLs like follows:


Example: /rj/rio-de-janeiro This example should give me 2 parameters: $uf ('rj') and $city ('rio-de-janeiro')

Another URL possible is:


Example: /rj/3 This example should give me 2 parameters: $uf ('rj') and $page (3)

In other words, the parameters "city" and "page" are optionals. I can't pass something like '/uf/city/page'. I need always or 'city' OR 'page'. But I don't know how to configure these routes in CodeIgniter configuration to point to same method (or even to different methods).

I've found the correct result:

$route['controllername/(:any)/(:any)/(:num)'] = 'ddd/index/$1/$2/$3';
$route['controllername/(:any)/(:num)'] = 'ddd/index/$1/null/$2'; // try 'null' or '0' (zero)
$route['controllername/(:any)'] = 'ddd/index/$1';

The Index method (inside "ControllerName") should be:

public function Index($uf = '', $slug = '', $pag = 0)
    // some code...

    if (intval($pag) > 0)
        // pagination

    if (!empty($slug))
        // slug manipulation

Hope it helps someone. Thank you all.

  • 15
Reply Report

For example, let’s say you have a URI like this:

  1. example.com/index.php/mycontroller/myfunction/hello/world
  2. example.com/index.php/mycontroller/myfunction/hello

Your method will be passed URI segments 3 and 4 (“hello” and “world”):

class MyController extends CI_Controller {

public function myFunction($notOptional, $optional = NULL)
    echo $notOptional; // will return 'hello'.
    echo $optional; // will return 'world' using the 1st URI and 'NULL' using the 2nd.


Reference: https://codeigniter.com/user_guide/general/controllers.html

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions