Welcome Guest, Not a member yet? Register   Sign In
is it ok to have repeated codes in controllers
#1

[eluser]rajneeshgobin[/eluser]
hello i am quite new in CI, have these tables in mysql

animal

hobby

and jobs


i have these models

animal_model

hobby_model

jobs_model

then i have the coresponding controllers

animalctrl,hobbyctrl jobsctrl

in all 3 controllers i have nearly same functions

such as insert,update ect

where i basically copied and pasted 3 times

is it ok to do like this

or i should have one controller for all 3 tables. then pass parameters to know which model to load ect.




#2

[eluser]CroNiX[/eluser]
You can create a base controller (MY_Controller), or base model (MY_Model) to do those, and then have each of your controllers extend those (or model). See this (extending core classes section): https://ellislab.com/codeigniter/user-gu...asses.html

You'd have something like:

Code:
class MY_Controller extends CI_Controller {
  function __construct()
  {
    parent::__construct();
  }

  public function insert($data_type, $data)
  {
    //insert $data based on $data_type (animal/hobby/jobs)
  }
  public function update($data_type, $data, $id)
  {
    //update $data based on $data_type where ID = $id
  }
}

Then you controllers would be like:
Code:
class Animal extends MY_Controller {
  function __construct()
  {
    parent::__construct();
  }

  function something()
  {
    //Use a method defined in MY_Controller
    $data = array(
      'name' => 'Cheetah',
      'location' => 'Africa'
    );
    $this->insert('animal', $data);
  }
}
#3

[eluser]treenef[/eluser]
Some people like to keep all their mysql queries in their models, this follows a more stricter MVC pattern.

But codeigniter is flexible in that you don't need to do this. Personally, I find having mysql code in my controllers easier to manage, that way I don't have to keep jumping through my files. Again this is personal preference. It is up to you.
#4

[eluser]Andrewkha[/eluser]
[quote author="CroNiX" date="1411401055"]You can create a base controller (MY_Controller), or base model (MY_Model) to do those, and then have each of your controllers extend those (or model). See this (extending core classes section): https://ellislab.com/codeigniter/user-gu...asses.html


}[/code][/quote]

So in this case the user will be able to type into address bar smth like http:\\site_name\MY_Controller\function_name\parameters

How to avoid this? I do not want the user gets access to these functions directly
#5

[eluser]InsiteFX[/eluser]
If you do not want them to get access then add

Code:
// The underscore makes it un-reachable from the url

public function _name()
{

}
#6

[eluser]Andrewkha[/eluser]
Thanks for the response. And one more question related to class extension.
If I extend, for example, Input class with newFunction. How do I need to access this newFunction?

$this->MY_input->newFunction? or the regular way?
#7

[eluser]InsiteFX[/eluser]
No, you would access it like any other method in the input class.

Code:
$this->input->newFunction();
#8

[eluser]Webmister[/eluser]
[quote author="rajneeshgobin" date="1411369932"]hello i am quite new in CI, have these tables in mysql

animal

hobby

and jobs


i have these models

animal_model

hobby_model

jobs_model

then i have the coresponding controllers

animalctrl,hobbyctrl jobsctrl

in all 3 controllers i have nearly same functions

such as insert,update ect

where i basically copied and pasted 3 times

is it ok to do like this

or i should have one controller for all 3 tables. then pass parameters to know which model to load ect.




[/quote]

Can i use one controller for all the view forms???
#9

[eluser]InsiteFX[/eluser]
Yes, but you will need to pass in a segment slug to do the view switching.




Theme © iAndrew 2016 - Forum software by © MyBB