Jump to content


Callback Functions - Where to place them in Codeigniter MVC Framework

callback mvc model controller

  • Please log in to reply
8 replies to this topic

#1 marlaaragao

marlaaragao

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 09 July 2015 - 04: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!



#2 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts
  • LocationUkraine

Posted 09 July 2015 - 08: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!



#3 marlaaragao

marlaaragao

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

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!



#4 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts
  • LocationUkraine

Posted 11 July 2015 - 11: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!



#5 buoncri

buoncri

    Member

  • Members
  • PipPip
  • 20 posts
  • LocationItaly

Posted 11 May 2016 - 01:38 PM

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

 

Cheers



#6 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

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 :)



#7 buoncri

buoncri

    Member

  • Members
  • PipPip
  • 20 posts
  • LocationItaly

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



#8 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

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.



#9 titu

titu

    Advanced Member

  • Members
  • PipPipPip
  • 43 posts

Posted 16 May 2016 - 12: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.







Also tagged with one or more of these keywords: callback, mvc, model, controller

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users