CodeIgniter Forums
404 issue with existing function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: 404 issue with existing function (/showthread.php?tid=49608)



404 issue with existing function - El Forum - 02-26-2012

[eluser]jsuissa[/eluser]
I'm getting 404 errors for an existing function with routes setup.

I've included the relevant routes and URL excerpts below. The function 'details' exists in the 'items' controller.

Any insights as to why this may be happening would be greatly appreciated.

Route (first works, second fails):
$route['item'] = 'items';
$route['item/(:any)'] = 'items/details';

URL fails for details function:
http://www.example.com/index.php/item/intro-to-8086-programming/1

URL works for index function:
http://www.example.com/index.php/item/


404 issue with existing function - El Forum - 02-26-2012

[eluser]Noobigniter[/eluser]
Assuming "1" is the id of the item.

Maybe like this, but given my level ... I'm not sure.
Code:
$route[‘item/(:any)/(:num)’] = ‘items/details/$2’;

Regards.


404 issue with existing function - El Forum - 02-26-2012

[eluser]CroNiX[/eluser]
Switch the route order from most segments to least.


404 issue with existing function - El Forum - 02-27-2012

[eluser]jsuissa[/eluser]
Thanks -- tried changing the route to:
$route[‘item/(:any)/(:num)’] = ‘items/details/$2’;

and also tried switching order:
$route['item/(:any)'] = 'items/details';
$route['item'] = 'items';

Still keep getting 404 error. I put a die() in the details function just to eliminate something inside the function as the problem and it never runs so it's not even getting to the function.

Below is the beginning of the controller if that helps:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

ini_set('display_errors',1);
error_reporting(E_ALL);


class Items extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model( 'items_model', 'Item' );
        $data['site_name'] = $this->config->item( 'site_name' );
        $this->load->vars( $data );
}

    public function index() {
        $data['page_title'] = 'All Items';
  $data['items'] = $this->Item->get_all();
  $this->load->view( 'templates/header', $data );
  $this->load->view( 'items/index', $data );
  $this->load->view( 'templates/footer', $data );
    }

public function details() { // ROUTE: item/{name}/{id}
  die("TEST");
  $id = $this->uri->segment( 3 );
  $item = $this->Item->get( $id );
  
  if ( ! $item ) {
    $this->session->set_flashdata( 'error', 'Item not found.' );
    redirect(site_url('items'));
  }
  
  $data['page_title'] = $item->name;
  $data['item'] = $item;
  
  $this->load->view( 'templates/header', $data );
  $this->load->view( 'items/details', $data );
  $this->load->view( 'templates/footer', $data );
}



404 issue with existing function - El Forum - 02-27-2012

[eluser]pickupman[/eluser]
I think CroNiX meant like:
Code:
$route['item/(:any)/(:num)'] = 'items/details/$2';
$route['item/(:any)'] = 'items/details/$1';
$route['item']        = 'items'; //Not needed but left for example purposes



404 issue with existing function - El Forum - 02-27-2012

[eluser]CroNiX[/eluser]
Try passing the id to the function.
Code:
$route['item/(:any)'] = 'items/details/$1';

Which can also be accessed (if you do this) as a parameter to your details() method.
Code:
public function details($id = FALSE)
{
  if ($id !== FALSE)  //default of FALSE, will be string if present
  {
     $id = (int)$id;  //change id from string to int
     //lookup $id, etc...
  }
}