dongmi8980 2016-10-25 13:35
浏览 27

路由时出错404

I'm trying to do my site like this tutorial http://www.codeigniter.com/user_guide/tutorial/static_pages.html

but have some problem with routing. I have page "createBook" for default and when I call localhost it's work! But when I do like localhost/createBook I have Error 404. What I'm doing wrong?

In my controller:

    public function view($page = 'createBook')
{
        if ( ! file_exists(APPPATH.'views/'.$page.'.php'))
        {
                // Whoops, we don't have a page for that!
                show_404();
        }
        $this->load->view($page);
}

routes.php file

$route['default_controller'] = 'Books/view';
$route['(:any)'] = 'Books/view/$1';

And I have view files in my views folder named Success and createBook

  • 写回答

1条回答 默认 最新

  • douguyi3903 2016-10-26 00:04
    关注

    I don't really get your question but it seems to me that you are trying to call a controller named createBook from the url, but in your function it shows that if there is no views/createBook.php than show 404, maybe its why you get a 404, because your calling a controller as a view, i think your function should be like this :

    public function view($page = 'createBook')
    {
      if ( ! file_exists(APPPATH.'controllers/' . $page . '.php'))
        {
         // Whoops, we don't have a page for that!
         $this->output->set_status_header('404');
         show_404();
        }
      $this->load->view($page);
    }
    

    If you want your url to look something like this :

    example.com/view/book/3
    

    Your controller should look like this :

    class View extends CI_Controller 
    {
      public function book($book_id) 
        {
         //Search the database for records about a book with its id, and return the data
         //and assign it to a variable(in this case $data) ex : $data["book-name"] = ...
         //And on your view you can call these values using ex : echo $book-name
         $this->load->view('books_view', $data);
        }
    }
    

    And make sure your .htaccess file next to the index.php looks like something like this :

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php/$0 [PT,L]
    

    Then go to your config, and change this line to :

    $config['index_page'] = '';
    

    Now you can access all of your controllers this way :

    http://www.example.com/controller_name
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题