CodeIgniter Forums
Hooks to edit controller function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Hooks to edit controller function (/showthread.php?tid=52530)



Hooks to edit controller function - El Forum - 06-14-2012

[eluser]manix[/eluser]
Hi there,

Well, I'm try to create a plugin system based on hooks, but I'm so confused in how hooks could alter a specific controller function, look the example below:

Code:
class Article extends CI_Controller
{
public function index()
{
  $title = $this->input->post('title');
  $body = $this->input->post('body');
  
  //try to add a new line here using hooks
  //maybe to add a new property like:
  //$published = $this->input->post('date')

}
}

How can I add a new line after where I have a commented line? I have tried with hooks but not good results. Additionally, I thought that using hooks is the best way to create a plugin system without changing the core code.

Thanks in advance


Hooks to edit controller function - El Forum - 06-17-2012

[eluser]manix[/eluser]
OK, I found the answer (at least one option).You need to set "hooks point" as many you need, in my case I did it by this way:

Code:
class Article extends CI_Controller
{
public function index()
{
  $title = $this->input->post('title');
  $body = $this->input->post('body');
  
  $article = new Article();
  $article->setTitle($title);
  $article->setBody($body);

  do_action('before_persist', $article);

  $this->my_db->save($article);

}
}


Finally, my "before_persist" function have the correct code in order to add new properties to $article object
Code:
$register_hook('before_persist', 'add_date_article', $params);

Code:
function add_date_article($params)
{
   $article = $params[0];
   $published = $this->input->post('date');
   $article->setPublished($publiched);
}