我在下面发布的代码工作正常,页面显示正确。
控制器:
class Articles extends Frontend_Controller {
public function __construct() {
parent::__construct();
$this->homeMetaTags = $this->configOptionsModel->get(1);
$this->bgimage = $this->configOptionsModel->get(2);
$this->db->where('catid = "1"');
$this->services = $this->articlesModel->get();
$this->projects = $this->projectsModel->get();
}
public function displayArticle()
{
$data['default_metatags'] = $this->homeMetaTags;
$data['bgimage'] = $this->bgimage;
$data['services'] = $this->services;
$data['projects'] = $this->projects;
$this->load->model('articlesModel');
$artid = $this->uri->segment(3);
$this->db->where('artid ='.$artid);
$data['article'] = $this->articlesModel->get();
$data['main_content'] = 'articles';
$this->load->model('articlesImagesModel');
$this->db->where('artid ='.$artid);
$data['artimg'] = $this->articlesImagesModel->get();
$this->load->view('frontEnd/template',$data);
}
}
看法:
<?php echo $article[0]['title'];?>
<?php echo $article[0]['articletext'];?>
在地址栏中显示以下网址:
www.mysite.com/articles/displayArticle/3 到目前为止,一切都很好。
但是......如果我在地址栏中输入:www.mysite.com/articles/displayArticle/3546666
我没有被重定向到自定义 404 错误页面,而是收到以下错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: frontEnd/articles.php
Line Number: 12
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: frontEnd/articles.php
Line Number: 48
果然,第 12 行和第 48 行是视图中的代码所在的两行。
我想如果有人键入不存在的 id 以重定向到 404 页面而不是看到这些错误。有人可以帮忙吗?
问候,佐兰