Codeignite - Why doesn’t my route work?

Asked

Viewed 86 times

1

I have the following route in Routes.php:

$route['xxxxx/(:any)'] = 'Order/index/$1';

And the following index in the Order controller:

public function index(){
    switch( $this->uri->segment(2)){
      case $this->step2:
        $this->motivation();
      break;
      case $this->step3:
        $this->about();
      break;
      case $this->status:
        $this->status();
      break;
      case 'post':
        $this->post();
      break;
      case 'teste':
        $this->teste();
      break;
      default:
        $this->register();
      break;
    }
  }

But when I try to enter this route by typing localhost/xxxxx for example, it returns me error 404 Page Not Found.

  • And if you access localhost/xxxxx/teste?

  • @Andersoncarloswoss works there

  • Apparently it only works if I pass a second segment

  • And localhost/xxxxx/, with the bar at the end?

  • With the bar at the end n works too, same thing

  • How’s the function? of this controller?

Show 1 more comment

1 answer

2


If you want an optional parameter you must then create two route settings, the Framework has no other form (is limited in that respect), then do:

Example:

$route['xxxxx'] = 'Order/index';
$route['xxxxx/(:any)'] = 'Order/index/$1';

Reference: URI Routing

Also worth remembering for this to work smoothly the parameter that is optional must have some default value, example:

class Order extends CI_Controller
{
    public function index($id = null)
    {
        //code
    }
}

in the example case the two routes work for this method!

  • That’s right, thank you, I made an if on the index to treat in case the second segment does not exist

Browser other questions tagged

You are not signed in. Login or sign up in order to post.