如何用codeigniter“新闻”部分解决页面空白的问题?

时间:2019-06-05 06:25:37

标签: php codeigniter-3

我正在关注Codeigniter教程:http://localhost:8080/myproject/user_guide/tutorial/news_section.html

如本节最后所述,当我将浏览器指向http://localhost:8080/myproject/news时,“将浏览器指向文档根目录,然后指向index.php / news并观看新闻页面。”出现空白页。

我试图指向localhost:8080 / index.php / news或/ myproject / news,但出现相同的问题。

我也尝试在autoload.php中设置下一个:$ autoload ['libraries'] = array('database'); 如codeigniter news section tutorial not working所示,但没有解决,因此我将其保留为:$ autoload ['libraries'] = array('');

这是路线:

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

News.php:

   <?php
   class News extends CI_Controller {

    public function __construct()
    {
            parent::__construct();
            $this->load->model('news_model');
            $this->load->helper('url_helper');
    }

    public function index()
    {
            $data['news'] = $this->news_model->get_news();
    }

    public function view($slug = NULL)
   {
    $data['news_item'] = $this->news_model->get_news($slug);

    if (empty($data['news_item']))
    {
            show_404();
    }

    $data['title'] = $data['news_item']['title'];

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

view.php:

   <?php
   echo '<h2>'.$news_item['title'].'</h2>';
   echo $news_item['text'];

和News_model.php:

   <?php
   class News_model extends CI_Model {

    public function __construct()
    {
            $this->load->database();
    }
    public function get_news($slug = FALSE)
    {
    if ($slug === FALSE)
    {
            $query = $this->db->get('news');
            return $query->result_array();
    }

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

我希望观看新闻页面

0 个答案:

没有答案