如何改进我的Codeigniter代码?

时间:2013-04-22 08:54:01

标签: codeigniter

我发布的代码很好,并且页面显示正确。

控制器:

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

的问候,卓然

3 个答案:

答案 0 :(得分: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'];?>

这应该可以正常工作

答案 1 :(得分: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;
 }  

答案 2 :(得分:0)

1)您必须重新考虑代码。模型代码不应该像控制器那样混乱(get,where functions)。你应该通过传递一些值来处理模型本身。

2)在这一行'$ this-&gt; db-&gt; where('artid ='。$ artid);'你应该在传递这个函数之前验证$ artid。

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

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

5)如果您需要常用功能,请将其保留在帮助程序中并调用函数名称。不要将整个代码保留在构造函数中。这是最好的做法。

如果您还需要其他任何东西,请问我。谢谢。我希望这会对你有所帮助。