Welcome Guest, Not a member yet? Register   Sign In
Hooks to edit controller function
#1

[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
#2

[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);
}







Theme © iAndrew 2016 - Forum software by © MyBB