Welcome Guest, Not a member yet? Register   Sign In
Pass current controller to Loader library.
#1

[eluser]Bramme[/eluser]
Hey all,

I'm trying to extend the Loader library a little. I wanna add some functionality that will autoload libraries, EXCEPT for the ones you set in the autoload.php file.

I've got a fair idea of how to approach this, though I'm currently stuck with the following: for this trick to work I need to know what Controller the visitor is currently viewing, to check wether to load a certain library or not.

I've added this piece of code to the Loader library (offcourse in a MY_Loader class), in the _ci_autoloader method:
Code:
if (isset($autoload_exclude))
{
    if (isset($autoload_exclude['libraries']))
    {
        foreach($autoload_exclude['libraries']['load'] as $item)
        {
            if (in_array($current_controller, $autoload['libraries']['exclude'])
            {
                $this->library($item);
            }
        }
    }
}
Offcourse, $current_controller is not set, since I don't know how to get it. I've been digging a bit through the source code, and I suspect the code I need is either in the Router class, or the Controller class. But it's 0:30 am and I'm getting a tad confused.

I was hoping that, when I got up tomorrow, someone could've hinted me in the right direction, so I know where to look.

Thanks in advance!
#2

[eluser]Bramme[/eluser]
The reason why I needed this code was because I need to autoload a few libraries in all but one controllers.

Today, I realised I once read someone suggesting "extending" the controller class and working from there. This seemed like a good idea to me:

In my libraries folder, I created an Admin_controller that extends controller. In the Admin_controller construct I load the libraries and then I adjusted my general.php controller to
Code:
require_once(APPPATH.'/libraries/Admin_controller.php');

class General extends Admin_controller {
}

I was just wondering if this was a good way of approaching this problem, or if there are better ways.
#3

[eluser]Colin Williams[/eluser]
I think people generally do this in application/libraries/MY_Controller.php

I could be wrong though.
#4

[eluser]Bramme[/eluser]
True, but then it's an autoloader again... Now I can extend from Admin_controller for admin controllers where the necessary libraries get loaded and I can extend from Controller for my content controller...
#5

[eluser]Colin Williams[/eluser]
If I were you, I'd just do some logic in config/autoload.php. Parse the url, set libraries for the one condition and then set libraries for other conditions:

Code:
$autoload['libraries'] = array('all1', 'all2', 'notadmin');
if ($first_segment == 'admin')
{
   array_pop($autoload['libraries']);
}

No need to go crazy with multiple base controllers and whatnot.
#6

[eluser]wiredesignz[/eluser]
PHP5?, Yes, check out spl_autoload_register('autoload').
Code:
public function autoload($class)
{
    if (is_file($filename = APPPATH.'libraries/'.$class.EXT))
            
        include_once $filename;
}

Code:
class General extends Admin_controller /* Admin_controller class is loaded automatically */
{
    public function __construct()
    {
        parent::__construct();
    }
}
#7

[eluser]Bramme[/eluser]
Yes it's php5, but I tried that code of yours wiredesignz, but then it said it didn't find the file Admin_controller...

That thing Colin is on about looks pretty promising, but how do I pass $first_segment to the config file?

Edit: besides, there's some other construct magic that needs to happen for all admin controllers (some queries etc), so I think I'll stay with my current solution...
#8

[eluser]Colin Williams[/eluser]
Quote:but how do I pass $first_segment to the config file

I knew that was coming Smile You'd have to parse $_SERVER['REQUEST_URI'] and generate it yourself.
#9

[eluser]wiredesignz[/eluser]
[quote author="Bramme" date="1217533324"]Yes it's php5, but I tried that code of yours wiredesignz, but then it said it didn't find the file Admin_controller...[/quote]

Did you add this line of code somewhere after the function: spl_autoload_register(’autoload’); probably both in at the bottom of config.php would be enough.
#10

[eluser]Bramme[/eluser]
Thanks a bunch Colin and wiredesignz for your help. Though somewhere I don't like the idea of adding logic, or even just a function to my config.php file... It just doesn't feel right imo.

I like the idea of extending the base controller much more. Plus, as said before, I need to do some other logic in all my constructors.

I don't really mind the fact I had to include the class file...


Smth I just thought off however: I could do the documented extending, MY_Controller etc and then just use some logic to check if the controllers are part of the admin subdirectory. That would be even better, no?




Theme © iAndrew 2016 - Forum software by © MyBB