⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Callback Functions - Where to place them in Codeigniter MVC Framework



marlaaragao
  • profile picture
  • Member

Posted 09 July 2015 - 16:04 PM

Hello!

 

I was hoping someone could help me with this question: Where to put callback funtions in codeiniter MVC framework?

 

When I need to use grocery crud's callback functions, I put them in the controller class, but I don't know if that's the best practice. So: what is the correct or simply the best place to put callbacks: Controllers ou Models?

 

Thanks a lot!


Paul Savostin
  • profile picture
  • Member

Posted 09 July 2015 - 20:06 PM

Hi! I am interesting where in grocery crud you using models?

For example, I am not using any models in GC app site and all callbacks at the same place in controller with prefix _ that you cant access it directly with http request from browser!


marlaaragao
  • profile picture
  • Member

Posted 11 July 2015 - 12:09 PM

Hi!

 

I do the same way as you.. But my application has models to some of the classes, the ones that have to deal with some more complex queries, some especific db actions. I thought that callback functions (the ones that save data in another table, for example) should be placed in theses Model Classes, but now I see it's not really worth it, or useful.. I'm going to keep puting them in the Controller classes..

 

Thank you for your reply!


Paul Savostin
  • profile picture
  • Member

Posted 11 July 2015 - 23:23 PM

Anyway, if I make some additional actions in callbacks using other models or ovveride callback_insert or callback_update by myself - everything goes in current controller so no need to set callbacks in other place.

For example, I have admin controller that handle users table. I have ovveride insert and update and using auth library (ion_auth) for register and update user. So all happens only in this controller place:)

You're welcome!


buoncri
  • profile picture
  • Member

Posted 11 May 2016 - 13:38 PM

This is what i found on codeigniter forums and can be intresting ...

 

Cheers


Amit Shah
  • profile picture
  • Member

Posted 12 May 2016 - 05:44 AM

Hello friends,

 

this is an interesting topic as what should be the best practice for the same... i too had been placing up all the callback functions in controllers .. but then my controller class tends to add up to a lot of lines of code .... as i have a quite some customizations to each ...

What better way i found was - most of the callback codes (except the callback for validations) ... can be placed down in a helper file - this will ease the seperation of responsibility and also have splitted code base across...

 

Happy GCing :)


buoncri
  • profile picture
  • Member

Posted 12 May 2016 - 07:35 AM

Amit Shaha

 

what you sad is what i am going to do becouse i have to share some callback on more controllers. May you give me suggestion on how to do or better a little example of how do you use (link) the helper in the controller ?

 

Many thanks in avdice :-D


Amit Shah
  • profile picture
  • Member

Posted 12 May 2016 - 10:27 AM

Hi Buoncri ...

 

Well from what i read ... you plan to have some shared callbacks. This can surely be dealt with a shared_callback_helper. Rest - say i am having a controller to handle all products related stuff, i can have a products_callback_helper - where i will be storing in all the callbacks for that controller. 

 

This way - i find it easy to handle the code separately.

 

Hope i made my statements clear.


titu
  • profile picture
  • Member

Posted 16 May 2016 - 00:57 AM

I stoped using callbacks, instead I starting using custom models, e.g.:

	public function db_delete($primary_key_value){


		    

			$ret = parent::db_delete($primary_key_value);

			$this->refresh($primary_key_value); 
			return $ret;
	}

I definitely recomend this approach for medium size projects.