0

http://mysite/products/create

Not Found

The requested URL /products/create was not found on this server.
Apache/2.2.16 (Debian) Server at site5.example.com Port 80

routes.php =>

$route['default_controller'] = 'products';
$route['404_override'] = '';

model =>

<?php
class Products_model extends CI_Model {

    function __construct()
    {
        $this->load->database();

    }

    function get_products($slug = FALSE)
    {
        if ($slug === FALSE)
        {
            $query = $this->db->get('products');
            return $query->result_array();

        }

        $query = $this->db->get_where('products', array('slug' => $slug));
        return $query->row_array();
    }

    function set_products()
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'text' => $this->input->post('text')
        );

        return $this->db->insert('products', $data);

    }

}

controller =>

<?php
class Products extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->model('products_model');

    }

    function index()
    {
        $data['products'] = $this->products_model->get_products();
        $data['title'] = 'Products archive';
        $this->load->view('products/index', $data);
    }

    function view($slug)
    {
        $data['products'] = $this->news_model->get_news($slug);
    }

    function create()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['title'] = 'Create a products item';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'text', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            //$this->load->view('templates/header', $data); 
            $this->load->view('products/create');
            //$this->load->view('templates/footer');

        }
        else
        {
            $this->news_model->set_news();
            $this->load->view('products/success');
        }   
    }

}
?>

view =>

<h2>Create a news item</h2>

<?php echo validation_errors(); ?>

<?php echo form_open('products/create') ?>

    <label for="title">Title</label> 
    <input type="input" name="title" /><br />

    <label for="text">Text</label>
    <textarea name="text"></textarea><br />

    <input type="submit" name="submit" value="Create news item" /> 

</form>

Why did i get this error ? Where is the error ?

3
  • If you can reach that function by http://mysite/index.php/products/create, seems you should configure your htaccess at the first place. Because that typical mvc url, http://domain/controller/function doesnt need a route rule to works. Commented Aug 28, 2011 at 21:02
  • But for mysite/products/create i got error, Not Found The requested URL /products/create was not found on this server. Apache/2.2.16 (Debian) Server at site5.example.com Port 80 Commented Aug 29, 2011 at 2:48
  • did you have .htaccess set up? I believe you are try to remove index.php from your uri, and for that, you will need to set up a htaccess rewite rule. Commented Aug 29, 2011 at 11:40

2 Answers 2

1

Not like this:

$route['default_controller'] = 'products/index';
$route['products/create'] = 'products/create';
$route['404_override'] = '';

Do it like this:

$route['default_controller'] = 'products';
$route['404_override'] = '';

Do not specify method, because than CI will look for "index" controller in application/products folder which does not exists. If you specify path in route than it looks for a path.

Sign up to request clarification or add additional context in comments.

1 Comment

I changed the routes.php ,but for mysite/products/create i got error, Not Found The requested URL /products/create was not found on this server. Apache/2.2.16 (Debian) Server at site5.example.com Port 80
0

Just a reminder (esp to myself).

If you create something like this

$route['products-view'] = 'products'; //refer to products.php
$route['products-create'] = 'products/detail/create'; //refer to products/detail.php

you will face a routing error when trying to open products-create because you have a file name products.php, it will think there is a method named detail in the file. CI will open to products and there is no such method name detail and will throw error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.