2011-08-28 168 views
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'] = ''; 

模型=>

<?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); 

    } 

} 

控制器=>

<?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'); 
     } 
    } 

} 
?> 

查看=>

<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> 

为什么我得到这个错误?错误在哪里?

+0

如果你可以通过'http:// mysite/index.php/products/create'实现该功能,看起来你应该首先配置你的htaccess。因为那个典型的mvc url,'http:// domain/controller/function'不需要路由规则来工作。 – toopay

+0

但是对于http:// mysite/products/create,我得到错误,未找到 在此服务器上找不到请求的URL/products/create。 Apache/2.2.16(Debian)服务器在site5.example.com端口80 – shibly

+0

你有.htaccess设置?我相信你试图从你的uri中删除index.php,为此,你需要设置一个htaccess rewite规则。 – toopay

回答

1

不喜欢这样的:

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

做这样的:

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

不要指定方法,因为比CI将查找在应用/产品目录“指数”控制器,它不存在。如果您在路径中指定路径而不是寻找路径。

+0

我更改了routes.php,但对于http:// mysite/products/create,我得到错误,未找到 在此服务器上找不到请求的URL/products/create。 Apache5.2.16(Debian)服务器在site5.example.com端口80 – shibly