CodeIgniter无法打开视图

时间:2017-05-08 23:31:45

标签: codeigniter

我刚刚开始使用CodeIgniter,一个有趣的想法就是当我按下按钮"阅读更多"我收到错误" 404找不到页面找不到您请求的页面。" 只是查看代码内部但找不到任何错误 任何评论,帮忙?

帖子控制器

 <?php


class Posts extends CI_Controller{

    public function index($page='home'){

        $data['title']= 'Sve voznje';

        $data['posts'] = $this->Posts_model->get_posts();

        $this->load->view('templates/header');
        $this->load->view('posts/index',$data);
        $this->load->view('templates/footer');
    }

    public function view($mjesto_polaska=NULL){
        $data['posts'] = $this->Posts_model->get_posts($mjesto_polaska);
        if(empty($data['post'])){
            show_404();
        }
        $data['title'] = $data['mjesto_polaska'];

        $this->load->view('templates/header');
        $this->load->view('posts/view',$data);
        $this->load->view('templates/footer');
    }

    public function create(){
        $data['title'] ='Create Posts';

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

        if($this->form_validation->run()===FALSE){

            $this->load->view('templates/header');
            $this->load->view('posts/create');
            $this->load->view('templates/footer');

        }else {
            $this->Posts_model->create_post();
            redirect('posts');
        }

    }

    public function delete($id){
        $this->Posts_model->delete_post($id);
        redirect('posts');
    }
}

帖子模型

<?php
 class Posts_Model extends CI_Model{

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

    public function get_posts($mjesto_polaska=FALSE){
        if($mjesto_polaska === FALSE){
            $this->db->order_by('id','DESC');
            $query=$this->db->get('posts');
            return $query->result_array();
        }
        $query=$this->db->get_where('posts', array('mjesto_polaska' => $mjesto_polaska));
        return $query->row_array();
    }

    public function create_post(){
        $mjesto_polaska=url_title($this->input->post('title'));

        $data=array(
                'mjesto_polaska'=>$mjesto_polaska,
                'mjesto_odredista'=>$this->input->post('mjesto_odredista'),
                'datum_polaska'=>$this->input->post('datum_polaska'),
                'datum_povratka'=>$this->input->post('datum_povratka'),
                'cijena'=>$this->input->post('cijena'),
                'broj_mjesta'=>$this->input->post('broj_mjesta'),
                'opis'=>$this->input->post('opis'),


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

    public function delete_posts($id){
        $this->db->where('id',$id);
        $this->db->delete('posts');
        return true;
    }
 }

的index.php

<h2><?= $title?></h2>
<?php foreach($posts as $post) :?>

<h3><?php echo  $post['mjesto_polaska']; ?></h3>
<small class="post-date">Posted on: <?php echo $post['created_at']; ?></small><br>
<br>
<p><a class="btn btn-default"    href="<?php echo site_url('/posts'.$post['mjesto_polaska']);?>">Read More</a></p>
<p><a class="btn btn-default"    href="<?php echo site_url('/posts'.$post['id']);?>">Delete</a></p>
<?php endforeach;?>

view.php

<h2><?php echo $posts['title'];?></h2>
<small class="post-date">Posted on:<?php echo post['created_at'];?></small>

<div class="post-body">
<?php echo $post['body'];?>
</div>

<hr>
<?php form_open('/posts/delete'.$post['id']);?>
<input type="submit" value="Delete" class="btn btn-danger">
</form>

routes.php文件

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$route['posts/create']='posts/create';
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts']='posts/index/$1';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

1 个答案:

答案 0 :(得分:1)

我在文件中做了一些更改。您可以检查并告诉我它是否在那里工作。不要忘记在配置自动加载文件中加载urlform帮助程序,因为您在模型中也使用了某些(url_title)。也要在控制器中填充构造函数方法。

routes.php文件

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// default routes should be at first place
$route['default_controller'] = 'pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['posts']='posts/index/$1';
$route['posts/create']='posts/create';
$route['posts/(:any)'] = 'posts/view/$1';

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

index.php视图文件

<h2><?= $title?></h2>
<?php foreach($posts as $post) :?>

<h3><?php echo  $post['mjesto_polaska']; ?></h3>
<small class="post-date">Posted on: <?php echo $post['created_at']; ?>
</small><br>
<br>
<p><a class="btn btn-default"    href="<?php echo 
site_url('/posts/'.$post['mjesto_polaska']);?>">Read More</a></p>
<p><a class="btn btn-default"    href="<?php echo 
site_url('/posts/delete/'.$post['id']);?>">Delete</a></p>
<?php endforeach;?>

view.php视图文件

<h2><?php echo $posts['title'];?></h2>
<small class="post-date">Posted on:<?php echo post['created_at'];?>
</small>
<div class="post-body">
<?php echo $post['body'];?>
</div>
<hr>
<?php form_open('/posts/delete/'.$post['id']);?>
    <input type="submit" value="Delete" class="btn btn-danger">
</form>