开发者

shifting controller function to library in codeigniter

I am trying to put this function ((click_add)) in library so that i can call it from all the controllers. I already have get_ads() function in library. I tried various ways to shift the click_add(id) function to library and call it to view along with get_ads but doesn't work. Please help

function __construct() {
    parent::__construct();
    $this->load->library('ads');
    $this->load->model('MGlobal');
}


public function index(){
    $data['banner']= $this->ads->get_ads();
    $this->load->view('test',$data);
}

    //i want this in library but no 开发者_高级运维luck
    public function click_add($ads_id){
    $ads_site = $this->MGlobal->getAds($ads_id);
    $this->MGlobal->add_ads_view();
    redirect($ads_site['url']);
  }

//and views is like this

foreach($banner as $k=>$list){    
    echo anchor('test/click_add/'.$list['bannerid'],'<img src="'. $list['image']. '"/>');
}

please suggest me how do i achieve that with library


It's also important to remember the role of each part of the MVC pattern. In your click_add() method, it looks like you're rendering a view and causing a redirect. These are two things best suited for a controller rather than a library. Rendering views and redirecting are two things that must be a controller's responsibility, and indeed, you won't be able to access them through the URL, which is what you're trying to do here.

If you want to reuse this method across multiple controllers in your site, try creating a MY_Controller core class and extending your controllers from that. That way, any methods you define in the MY_Controller will be available in any controller you subclass from.

Without any specific error messages or a more verbose description of the problem you're having, I'm afraid there's little more help I can give you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜