使用CodeIgniter创建新闻项

时间:2017-12-03 13:23:51

标签: php codeigniter

我已经按照文档说明了,但是当我尝试创建新闻时,我收到错误:

"此页面无效www.mi-linux.wlv.ac.uk未发送任何数据。 ERR_EMPTY_RESPONSE"

我的基本网址:

$config['base_url'] = 'http://www.mi-linux.wlv.ac.uk/1043809/CodeIgniterNew/';

我的路线:

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

我的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();
}

public function set_news() {
    $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('news', $data);
}
}

我的database.php

$db['default'] = array(
'dsn'   => '',
'hostname' => 'localhost',
'username' => '1403809',
'password' => 'IhaveApassword',
'database' => 'db1403809',
'dbdriver' => 'mysqli',

这是我的控制器,我在controllers文件夹中有两个其他文件,一个名为pages.php,另一个名为Welcome.php

Controller 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();
    $data['title'] = 'News archive';

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

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

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

    $data['title'] = 'Create a news 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('news/create');
        $this->load->view('templates/footer');

    }
    else {
        $this->news_model->set_news();
        $this->index();
    }
}
}

1 个答案:

答案 0 :(得分:2)

嗯,我想这是你的服务器停机:

Screenshot of your site

只是为了确保你的代码不是很麻烦的本地PHP服务器并在本地主机上运行它。在Windows上说WAMPP或在Mac / Linux / Windows上说XAMPP。他们是免费的