Welcome Guest, Not a member yet? Register   Sign In
Beginners question... Functions?
#1

[eluser]Unknown[/eluser]
Hi, I am brand new to both Code Igniter and OOP so forgive me for the potentially stupid question.

If I am creating a site with multiple controllers and I want to write a function which I will be able to call from any of those controllers, where would I put the function?

Thanks a lot Smile
#2

[eluser]WanWizard[/eluser]
Preferably in a library, or if it has to be procedural, in a helper.
#3

[eluser]JHackamack[/eluser]
Depending on the function you can put it in a model too. To take a quote from the user guide:

Models are PHP classes that are designed to work with information in your database. For example, let's say you use CodeIgniter to manage a blog. You might have a model class that contains functions to insert, update, and retrieve your blog data.
#4

[eluser]WanWizard[/eluser]
If the function's purpose is to interact with data, then indeed a model would be a more logical choice.
#5

[eluser]Unknown[/eluser]
Thanks for the input guys, maybe it will help if I tell you what it does.

I'm building a facebook app and at the moment I have this throughout the controllers multiple times...

Code:
//Set facebook Config
        $data['facebook'] = new Facebook(array(
          'appId'  => '204141329601359',
          'secret' => '78070567a4a0de2b1cc9c6a2b493c2ce',
          'cookie' => true,
        ));
        
        // Get facebook session
        $data['session'] = $data['facebook']->getSession();
        
        // Try api call and get user id and user object
        $data['me'] = null;
        // Session based API call.
        if ($data['session']) {
          try {
            $data['uid'] = $data['facebook']->getUser();
            $data['me'] = $data['facebook']->api('/me');
          } catch (FacebookApiException $e) {
            error_log($e);
          }
        }

Where do you think the best place to put something like that would be?
#6

[eluser]InsiteFX[/eluser]
For api calls I place them in a library.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB