Codeignite - Why does not my route work?

1

I have the following route in routes.php:

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

And the following index in the controller Order:

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 .

    
asked by anonymous 23.05.2018 / 17:46

1 answer

2

If you want an optional parameter then you should create two route settings, Framework has no other way ( is limited in this respect ), then do:

Example:

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

Reference URI Routing

It is also worth remembering for this to work without problems the parameter that is optional should have some default value, eg:

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

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

    
23.05.2018 / 18:32