0

我在下面发布的代码工作正常,页面显示正确。

控制器:

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 页面而不是看到这些错误。有人可以帮忙吗?

问候,佐兰

4

3 回答 3

1
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);// What is this doing here. This should be in model
        $this->services         =   $this->articlesModel->get();
        $this->projects         =   $this->projectsModel->get();
    }

    public function displayArticle()
    {
        $this->load->model('articlesModel');
        $this->load->model('articlesImagesModel');

        $data['default_metatags']   =   $this->homeMetaTags;
        $data['bgimage']            =   $this->bgimage;
        $data['services']           =   $this->services;
        $data['projects']           =   $this->projects;

        $artid = $this->uri->segment(3);

        $this->db->where('artid',$artid);// What is this doing here. This should be in model

        $data['article']            =   $this->articlesModel->get();
        $data['main_content']       =   'articles';
        /*  What is it doing here */
        $this->db->where('artid ='.$artid);

        $data['artimg']             =   $this->articlesImagesModel->get();

        $this->load->view('frontEnd/template',$data);       
    }
}

我假设当你打电话时$this->articlesModel->get()你正在返回 row_array()而不是result_array(). 该函数返回对象而不是对象数组。将模型更改row_array()result_array()或在视图中

<?php echo $article['title'];?>
<?php echo $article['articletext'];?>

这应该可以正常工作

于 2013-04-22T09:03:51.053 回答
0

尝试正确使用 View Model Controller 模型。当前,您正在控制器中检索数据库访问权限。我建议你看这个开始。 http://www.youtube.com/watch?v=mWsAruzW3Jw

无论如何,要显示 404 页面,请使用以下命令:

show_404(); 

404处理程序:

 /**
* 404 Page Handler
*
* This function is similar to the show_error() function above
* However, instead of the standard error template it displays
* 404 errors.
*
* @access public
* @return void
*/
 function show_404($page = '', $log_error = TRUE)
 {
  $_error =& load_class('Exceptions', 'core');
  $_error->show_404($page, $log_error);
  exit;
 }  
于 2013-04-22T09:06:08.477 回答
0

1) 你必须重构你的代码。模型代码不应与(get、where 函数)之类的控制器混淆。您应该通过传递一些值在模型本身中处理这个问题。

2) 在这一行 '$this->db->where('artid ='.$artid);' 您应该在传入此函数之前验证 $artid。

3) 如果您收到这样的错误消息,请使用 try catch 语句显示 404 页面。

4) 如果您要重定向到 404 页面,您应该正确记录错误以便稍后修复。

5)如果您需要通用功能,请将其保存在助手中并调用函数名称。不要将整个代码保留在构造函数中。这是最佳实践。

如果你还需要什么,可以问我。谢谢。我希望这能帮到您。

于 2013-04-22T09:08:20.077 回答