Welcome Guest, Not a member yet? Register   Sign In
Autoloading library methods?
#11

[eluser]Wayne Smallman[/eluser]
[quote author="Phil Sturgeon" date="1296508549"]If you give me some examples perhaps I could give you a more accurate answer.

This will not make something run on EVERY controller, only those that extend a type of base controller. By giving your controllers types you specify what sort of functions should be run by default.

Admin_Controller extends MY_Controller
Public_Controller extends MY_Controller
AJAX_Controller extends MY_Controller
WHYWSIWYG_Controller extends Admin_Controller

You can create all sorts of combination's for the various types of functionality parts of your site needs.[/quote]OK, I've got a basic messaging system that informs users that someone has commented on a relationship, which is a collection of bookmarks.

I want an appropriate number to appear next to the "Messages" navigation item, informing the user how many messages they have that are unread.

This is just one example, but as the application develops, there will be countless others.

I'm using the simplest "MY_Controller" method, which extends the core controller and all my controllers then look to the new "MY_Controller". Within that controller, I'll place all of my functions which I want to load automatically.

So far, I can't do that, and the only way I can get any of this to work is to add the code into the constructor for each controller, which is pointless, as I could have just as easily accomplished all of this without messing around adding an middle man of a controller.

Any ideas?
#12

[eluser]Phil Sturgeon[/eluser]
Right, a Navigation would be part of the adnmin panel, so you make an Admin_Controller. This is not pointless messing around. you assign $this->data['message_count'] in the base class then :

$this->load->view('whatever', $this->data);

The base class is more effective than hooks as you cannot really pass data around as well in a hook. You could do $this->load->vars('message_count', $message_count) but that is a bit too "magic".

Use base controllers, they will be insanely useful to you as you move forwards.
#13

[eluser]Wayne Smallman[/eluser]
[quote author="Phil Sturgeon" date="1296510769"]Right, a Navigation would be part of the adnmin panel, so you make an Admin_Controller. This is not pointless messing around. you assign $this->data['message_count'] in the base class then :

$this->load->view('whatever', $this->data);

The base class is more effective than hooks as you cannot really pass data around as well in a hook. You could do $this->load->vars('message_count', $message_count) but that is a bit too "magic".

Use base controllers, they will be insanely useful to you as you move forwards.[/quote]
Hi Phil. I know what you're getting at, but I've already defined the views for the navigation, so I'd have to remove them (of which there are many) all to feel the benefit of your technique.

In hindsight, had I known about this technique before starting work, I would be fully appreciating those benefits. I'll have a read through your documentation to see whether it's going to be practical to do what you suggest.

The problem is, though, views (amongst other things) aren't always going to be the same, and some pages (like the page for the web browser button when adding bookmarks) won't need a view for the navigation.

So rather than reduce complexity, I'd most likely be increasing it and totally overloading the application from the get go with umpteen checks for this, that and t'other.
#14

[eluser]Phil Sturgeon[/eluser]
You aren't quite understanding me.

Ok, so for this example the messages are in the admin panel.

Admin_Controller:

Code:
class Admin_Controller extends MY_Controller {

    function __construct()
    {
        parent::__construct();

        $this->data['message_count'] = count($this->message_m->get_all());
    }

}

That makes the data available to any controller that uses Admin_Controller. So now we make our controllers use it:

Code:
class Blog extends Admin_Controller {

    function index()
    {
        $this->load->views('whatever', $this->data);
    }

}

No change to your views, just how the controllers are called. Add a __autoload() function like the article says and you're good to go.
#15

[eluser]Wayne Smallman[/eluser]
[quote author="Phil Sturgeon" date="1296517652"]You aren't quite understanding me.

Ok, so for this example the messages are in the admin panel.

Admin_Controller:

Code:
class Admin_Controller extends MY_Controller {

    function __construct()
    {
        parent::__construct();

        $this->data['message_count'] = count($this->message_m->get_all());
    }

}

That makes the data available to any controller that uses Admin_Controller. So now we make our controllers use it:

Code:
class Blog extends Admin_Controller {

    function index()
    {
        $this->load->views('whatever', $this->data);
    }

}

No change to your views, just how the controllers are called. Add a __autoload() function like the article says and you're good to go.[/quote]Hi Phil, I understand you completely; I think it's the other way around.

To use that technique, I would have to remove all of the calls to those views it's replacing, or there would be a mass duplication of navigational devices throughout the entire website. And as I said earlier, I'd still have to perform various checks for those situations where the navigation isn't needed all.

I do remember seeing your article on the "MY_Controller" technique a while ago, and the technique itself struck me as being insubstantial, when there really should be some proper means of not just automatically loading a library or a controller, but of functions, too.

I'll figure something out myself because this technique really isn't viable.
#16

[eluser]Phil Sturgeon[/eluser]
"all of the calls to those views it’s replacing"

What are you talking about? Why would there be loads of navigational devices? This is purely to load specific data before controllers and execute global code (which all systems need to do at some point.

Do login checks in admin, extra user data assigned to views frontend. You only load views in your controller, and those views now have access to the data they need because the data is gobal.

You sound like a smart guy but either you think you understand this and you don't, or you are describing why it is not appropriate really badly.




Theme © iAndrew 2016 - Forum software by © MyBB