Authorize specific CodeIgniter classes

I created two controllers: Public_Controller and Admin_Controller in the folder. / application / libraries, following the example of Phil Sturgeon.

What I want to do is automatically load Public_Controller and Admin_Controller, so I created this autoload function inside. /application/config.php

function __autoload($class) {

    // Autoload only Public_Controller and Admin_Controller
    if (strpos($class, 'CI_') !== 0) {
        $file = APPPATH . 'libraries/'. $class .'.php';
        if ( file_exists($file) && is_file($file) ) {
            @include_once($file);
        }
    }
}

The problem with this, I think, is that I have more files included in the libraries folder, so they also load automatically, which is not what I want. So instead, I tried to make a small change for the first if statement, for example:

if ( in_array($class, array('Public_Controller, Admin_Controller')) ) // instead of strpos

to focus only on these two classes, but that doesn't seem to work. Any ideas what I can do wrong?

+4
5

Public_Controller Admin_Controller admin, autoload.php . autoload.php . __autoload() , .

0

applications/config/autoload.php, , .

packages, libraries, helpers, config, languages models.

,

$autoload['libraries'] = array('database', 'session');

$autoload['helper'] = array('url', 'html', 'form');
+4

codeigniter ,

"autoload.php" application/config/directory

0

U ..codeiniter ibuilt dunk ...

application/config/autoload.php,

$autoload['libraries'] = array('database', 'session','your-specific_file');
-1

. __autoload() . , PHP,

__autoload() - undefined. "" (init) : "include, include_once, require, require_once".

, .

Codeigniter autoload

open the file application / config / autoload.php and add the item that you want to load into the startup array. You will find instructions in this file for each type of item. - Codeigniter Documents

Hope this helps :)

-1
source

Source: https://habr.com/ru/post/1530593/


All Articles