【发布时间】:2016-11-16 05:18:00
【问题描述】:
我从未使用过 PHP 框架,但我想学习如何使用它以便我可以更轻松地为我的公司编程,所以我选择了 CodeIgniter。
我正在关注新闻部分的代码点火器教程,以便了解事情的工作原理 - 但我遇到了一些问题。
现在,我在教程中指出我有一个提交并带我进入成功页面的表单。在普通 PHP 中,这就是我将页面重定向到提交的新闻项目的查看页面的地方。在 CodeIgniter 中,我的控制器中有一个视图函数
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');
}
我不确定在提交时如何从表单中获取 slug。
这是我的创建函数(基本上是插入数据库信息并重定向到news/success的函数)
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->load->view('news/success');
}
}
但我希望它重定向到视图页面。为此,我需要拥有我相信的蛞蝓。所以它应该重定向到news/view/$slug - 但我不知道该怎么做。
我的 set_news 模型:
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);
}
【问题讨论】:
标签: php codeigniter