• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending data from one controller to another

#1
Hi

I need to send data from one controller to another.

Now from what I've read, I shouldn't need to do this, but I think I do. I'll explain why.

I have an application that has an activity log, so I need to log every transaction made by every controller. Logic tells me I should have an LOG controller which processes each entry and then displays when required etc.

SO to achieve this, after each transaction that is performed by each controller, I would then need to pass that data to the LOG controller for entry into the log table.

Currently I am sending data from each controller method to the LOG Model for processing. So this involves setting the date, serializing data etc.

BUT surely it would be better to send the data from each controller to a LOG controller?

If that's even possible, how would i achieve that?

Your thoughts

Thanks
Alan
Reply

#2
So how is "pass that data to the LOG controller for entry into the log table" different than "each controller method to the LOG Model for processing"

Using the model I assume you have something like:
$this->log_model->add('FOO BAR');

Where your hoping it could be something like?
$this->call_controller('controller_name','controller_method','FOOBAR');

I am confused as to why this needs to be a controller method call?

DMyers
Reply

#3
OK

My issue is purely thinking about future proofing the application.

Lets say my application performs 100 different transactions from 30 different controllers.

Now all of a sudden I want to add an extra column to the audit log database table, lets say the users browser. I would then have to amend all 100 transactions to include the additional column.

If I had a LOG controller with an add_log method, I could add the column easily.
Reply

#4
Create a model that performs the operation in your database.
Autoload the model in application/config/autoload.php
From any controller:
PHP Code:
$this->mylog_model->writelog(); 

If you want to be flexible to what data is passed to the model's method, use an array, e.g.
PHP Code:
$this->mylog_model->writelog($data);   //$data is an array that is processed by your model. 
Reply

#5
Just off the top of my head I would probably add a "type" parameter

Code:
$this->mylog_model->writelog('controller','Read Active Customer IDs');

Then in the write log method I could make it do all kinds of special magic based on the type.

For example run a debug_backtrace() and grab all kinds of stuff about who called you as well as anything available to the CI Input Class, URI Class objects, User Agent Class, Session Library, etc...

Code:
$this->mylog_model->writelog('model','Read Blog Entry');

this might grab the database stats or something since you specified it as a model type?

Code:
$this->mylog_model->writelog('generic','Read Blog Entry 23');

Do nothing special but log that entry.

That of course could also be a column in itself called "type" which you could filter on later.

You could also just send in a instance of who ever is calling the method ie.

Code:
$this->mylog_model->writelog($this,'Do Something');

Then you could look at the $this parameter in the method and determine what to do? Everything (public) available to the caller would be available to the writelog method.

DMyers
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.