Welcome Guest, Not a member yet? Register   Sign In
Making calls to functions within a single controller
#1

[eluser]Wittner[/eluser]
Just wondering what the framework 'rule' is on the following:

I have been using Codeigniter now for a couple of months and it has changed the way I produce my code beyond recognition. I'm a procedural programmer so there has been quite a bit to learn about the CI way of working. I'm now a convert and really enjoying coding with it.

I'm wondering if it is advisable to call a function in a controller from a function within the same controller? For example, I have a rather complex property booking system which I've written. In a controller, when an edit function is called, it consists of the following type of scenario(pseudo code):

Code:
data['blah'] = this->blah_model->get_charges
data['blah'] = this->blah_model->get_payments
...
data['blah'] = this->blah_model->get_booking($bookingCode)
load->view('edit_view')

My question is - every time I want to call the edit function from a controller, I have to use a few lines to gather all the associated data and then call the edit view - can I make those lines part of a seperate 'edit' function within a controller and call it from that controller or indeed any other controller, thus:

(in a controller)

Code:
function whatever()
{
$bookingCode = $this->input->post('bookingCode')
... Do some stuff
edit_booking($bookingCode)
}

function edit_booking($bookingCode)
{
data['blah'] = this->blah_model->get_charges
data['blah'] = this->blah_model->get_payments
...
data['blah'] = this->blah_model->get_booking($bookingCode)
load->view('edit_view')
}

Hope that's clear and that someone can help. Codeigniter has reduced my bloated code so much maybe I'm just getting greedy!

;-)

cheers,

Wittner
#2

[eluser]Bramme[/eluser]
Yes you can, however, you're syntax is a little bit wrong (really, just a tiny bit) and there's a CI way of doing this (the edit function) better.

your code should change into
Code:
function whatever()
{
$bookingCode = $this->input->post('bookingCode')
... Do some stuff
$this->_edit_booking($bookingCode)
}

function _edit_booking($bookingCode)
{
data['blah'] = this->blah_model->get_charges
data['blah'] = this->blah_model->get_payments
...
data['blah'] = this->blah_model->get_booking($bookingCode)
load->view('edit_view')
}
Check the user guide for private functions Wink
#3

[eluser]Wittner[/eluser]
Hi Bramme,

hah! The old private function eh? Great that I can do this and thanks for the tip. It was only sort of pseudo code but you brought the private function idea to mind also so thanks for both answers!!

:coolsmile:

cheers,

Wittner
#4

[eluser]Michael Wales[/eluser]
As Bramme said it's possible and using a CI private function is definitely the way to go about it.

Also, think about pulling this code out into a library or (in this particular case) the model.
#5

[eluser]Wittner[/eluser]
Thanks Michael,

Quote:think about pulling this code out into a library or (in this particular case) the model.

hmmm, you see, this is where I get a bit confused. I'm using the controller to call the various functions from the (booking) model. What I'm wondering is, to save using all the calls to the different functions each time, would it be more economical to make a new function (within the controller) with all the calls in it to save on replication? Or am I (probably!) missing something?

cheers,

Wittner
#6

[eluser]vanzl[/eluser]
You could make that function in the model. Your controller would tell the model what data it needs and the model would handle the rest. It would look something like this:

The Controller:
Code:
function _edit_booking($bookingCode){

$needed_data = array ('charges'=>true, 'payments'=>true, 'BookingCode'=> $BookingCode);

$data = this->blah_model->get_data($needed_data) //the new method within the controller

load->view('edit_view')
}
..and the model:
Code:
function get_data($needed_data){

    if ($needed_data['charges']){
     $data['charges'] = $this->get_charges();
    }

    if ($needed_data['payments']){
     $data['payments'] = $this->get_payments();
    }

    $data['booking'] = $this->get_booking($needed_data['BookingCode'])
    
    return $data;
}
#7

[eluser]Wittner[/eluser]
Thanks Vanzl. That's another possibility. It feels a little like using the model as a controller, but I can see how it might cut down some coding for sure - more food for thought!

cheers,

Mike
#8

[eluser]Crafter[/eluser]
[quote author="Wittner" date="1221621327"]

Quote:think about pulling this code out into a library or (in this particular case) the model.

hmmm, you see, this is where I get a bit confused. I'm using the controller to call the various functions from the (booking) model. What I'm wondering is, to save using all the calls to the different functions each time, would it be more economical to make a new function (within the controller) with all the calls in it to save on replication? Or am I (probably!) missing something?

[/quote]

I'm definitely no expert, but I would think that the overhead of calling a local function and a library function would be the same on the stack, so there probably would not be any difference whichever way you choose,

However, I would also expect a slight overhead in loading a library that otherwise not be loaded if the function was local (to the controller.

Personally, I would favour programmer convenience over performance here, unless my application has specific performance requirements.

Nice point, though.




Theme © iAndrew 2016 - Forum software by © MyBB