Welcome Guest, Not a member yet? Register   Sign In
Understanding Controllers, Libraries and Models
#1

[eluser]Unknown[/eluser]
So far I have used CI mostly as simple page include system and try now to dig further into it and ran into some general concept questions which I like to post here to better understand the concepts of controllers, models and libraries. Here we go:

1) Is a controller a singleton pattern? Like, one instance of a specific controller is created by CI and made available to all users that visit that controller? I'm wondering if a class variable of a controller would be a bad thing (like in Java). So would this variable "user" be unique to each visitor of this controller or would that be used by all users?

Code:
<?php
//require_once('facebook-platform/php/facebook.php');

class Game extends Controller {
        var $user = ""; // <--- IS THIS BAD?

        function Game()
        {
...
What I want to archive is just loading the facebook platform class ONCE only within the library so that all other controllers later don't need to care about, just load the lib and all is set.

2) I'm trying to wrap the Facebook client API for CI. I know there is a lib for that, but that doesn't work actually if you use CI in an iFrame solution within FB. What I did now is to create a library class to make the general facebook object available to all controllers. Is it correct to assume that a library is a singleton pattern and that it would be a bad thing to put user date into the library, like a user id? Or is the library class instantiated for each user browsing the controller that includes the library?

3) Is it "allowed" to use the "require_once" directive within a library class like this?

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('facebook-platform/php/facebook.php');

class FBWrapper {

    var $test = "Hello World";
    private $_api_key = NULL;
    private $_secret_key = NULL;
    private $_obj;
    public $fb;
    public $client;

4) Would it be correct to better store all user data directly in the session object? So, if a controller gets a request, check if there is a facebook user_id variable in the session, if not, create it and put it back into the session?

Thanks for your time! :-)
Martin
#2

[eluser]Ben Edmunds[/eluser]
Hey Martin,

With the way PHP and CI work each library should be loaded once per user per page request.

There is nothing wrong with declaring your variables at the class level if they are needed across all of the methods in the class.


If you need data to persist across multiple page requests then you will want to use session data.
#3

[eluser]Unknown[/eluser]
Awesome, thanks for the answer.




Theme © iAndrew 2016 - Forum software by © MyBB