Welcome Guest, Not a member yet? Register   Sign In
Codeigniter showing "404 page not found" error, code looks correct
#1

[eluser]MikeW1911[/eluser]
I am using Codeigniter 2.1.0 and following this tutorial shopping cart tutorial

The previous tutorials in the series worked fine, but when I load the controller for this tutorial, Codeigniter gives me the "404 page not found" error. I hope it's not a silly mistake

"shop.php" controller:

Code:
<?php

class Shop extends CI_Controller {

public function __construct(){
  parent::__construct();
  $this->load->model('model_products');
  $data['products'] = $this->model_products->get_all();
  
  $this->load->view('view_products', $data);
}
}

"model_products.php" model:

Code:
<?php

class Model_products extends CI_Model {

public function get_all(){
  
  $results = $this->db->get('ci_products')->result();
  
  foreach ($results as $result) {
  
   if ($result->option_values){
    $result->option_values = explode(',', $result->option_values);
   }
  }
  return $results;
}
}

"view_products.php" view:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
<div id="products">
     <ul>
         &lt;?php foreach($products as $product): ?&gt;
             <li>
                 &lt;?php echo form_open('shop/add'); ?&gt;
                     <div class="name" >&lt;?php echo $product->name; ?&gt;</div>
                        <div class="thumb" >
                         &lt;?php echo img(array(
        'src' => 'images/' . $product->image,
        'class' => 'thumb',
        'alt' => $product->name
       )); ?&gt;
                        </div>
                        <div class="price">&lt;?php echo $product->price; ?&gt;</div>
                    &lt;?php echo form_close(); ?&gt;
                </li>
            &lt;?php endforeach; ?&gt;
        </ul>
    </div>
    
    <div id="cart">
    
    </div>
&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]Aken[/eluser]
You need an index() method in your controller.
#3

[eluser]MikeW1911[/eluser]
Thank you, that worked! Why doesn't public function __construct() work properly for controllers?
#4

[eluser]Aken[/eluser]
The way CodeIgniter calls controllers based on the URL requires an index() method if you're accessing a controller's "home" page, like in your case. The constructor is a magic method run every time the class is instantiated, and any code inside of it is run before any method called. You should move the index-specific content to your index() method, and leave the constructor with just "parent::__construct();" in it.

Read more about CI controllers in the user guide, and PHP Object-Orientated Programming if you want to understand more (which is recommended if you want to use this framework).




Theme © iAndrew 2016 - Forum software by © MyBB