Welcome Guest, Not a member yet? Register   Sign In
404 issue with existing function
#1

[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/in...gramming/1

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

[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.
#3

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

[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 );
}
#5

[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
#6

[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...
  }
}




Theme © iAndrew 2016 - Forum software by © MyBB