超薄框架 - 如何自动加载Slim / Slim.php而不是使用require?

How can I autoload Slim/Slim.php instead of using require?

// standard method
//require 'ext/Slim/Slim.php'; 

// autoload method:     
define ('WEBSITE_DOCROOT', str_replace('\\', '/', dirname(__FILE__)).'/');

// Instance of SplAutoload.
$SplAutoload = new SplAutoload();

// Load classes.
$SplAutoload->fetch([
    'ext/' // Slim/ is kept under ext/
]);

\Slim\Slim::registerAutoloader();

//Instantiate a Slim application:
$app = new \Slim\Slim();

//Define a HTTP GET route:
$app->get('/', function () {
    echo "Hello!";
});

$app->get('/hello/:name/', function ($name) {
    echo "Hello, $name";
});

//Run the Slim application:
$app->run();

I get this error,

Fatal error: Class 'Slim\Slim' not found in C:...

the SplAutoload class,

class SplAutoload
{
    /**
     * Set the property.
     */
    public $directories;

    public function fetch($directories) 
    {
        // Store the data into the property.
        $this->directories = $directories;

        // Register the classes that were loaded by __autoload as well.
        spl_autoload_register('__autoload');

        // When using spl_autoload_register() with class methods, it might seem that it can use only public methods, 
        // though it can use private/protected methods as well, if registered from inside the class:
        spl_autoload_register([$this,'getClass']);
    }

    private function getClass($className)
    {
        if(is_array($this->directories)): $mainDirectories =  $this->directories;
        else: $mainDirectories =  array($this->directories); endif;

        // Set other vars and arrays.
        $subDirectories = [];
        //print_r($mainDirectories);

        $namespace = "\\";
        $isNamespace = false;

        // When you use namespace in a class, you get something like this when you auto load that class \foo\tidy.
        // So use explode to split the string and then get the last item in the exloded array.
        $parts = explode($namespace, $className);

        // Check if the current class is a namespace class or not.
        if(strpos($className, $namespace) !== false) 
        {
            $isNamespace = true;
        }

        // Set the class file name.
        $filename = end($parts).'.php';

        // List any sub dirs in the main dirs above and store them in an array.
        foreach($mainDirectories as $mainDirectory)
        {
            // Check if the directory exists.
            // Create it if it doesn't.
            if (!file_exists(WEBSITE_DOCROOT.$mainDirectory)) 
            {
                mkdir(WEBSITE_DOCROOT.$mainDirectory, 0777);
            }

            // Must use absolute path to get the files when ajax is used.
            foreach(glob(WEBSITE_DOCROOT.$mainDirectory.'*', GLOB_ONLYDIR) as $dir) 
            {
                // Must trim off the WEBSITE_DOCROOT. 
                $subDirectories[] = preg_replace('~.*?(?=core|local)~i', '', str_replace('\\', '/', $dir)) .'/';
            }
        }

        // Mearge the main dirs with any sub dirs in them.
        $mergedDirectories = array_merge($mainDirectories,$subDirectories);

        // Loop the merge array and include the classes in them.
        foreach($mergedDirectories as $mainDirectory)
        {
            if(file_exists(WEBSITE_DOCROOT.$mainDirectory.$filename))
            {
                include_once WEBSITE_DOCROOT.$mainDirectory.$filename;

                // Check if the class has existed.
                if($isNamespace === false) if (class_exists($className)) break;
            }
        }
    }
}

or any other SPL autoloader that I should go for? zend-autoloader?

doujingdai5521
doujingdai5521 我只是尝试了但仍然得到同样的错误:(请看看这里stackoverflow.com/questions/27826936/...
5 年多之前 回复
dongyan5815
dongyan5815 你也没有(虽然我会推荐它)...我只是在检查你是否忽略了如何从作曲家管理包中获得自动加载。
5 年多之前 回复
dqy27359
dqy27359 我可以试一试!:d
5 年多之前 回复
drtj40036
drtj40036 你在使用作曲家吗?
5 年多之前 回复

1个回答



使用自动加载器处理命名空间的方式,您需要使用它:</ p>

 <  code> $ SplAutoload-&gt; fetch([
'ext / Slim /'// Slim /保存在ext /
下]);
</ code> </ pre>

或 更改自动加载器,使其包含命名空间作为目录。</ p>
</ div>

展开原文

原文

With the way your autoloader handles namespaces, you'll need to use this:

$SplAutoload->fetch([
    'ext/Slim/' // Slim/ is kept under ext/
]);

or change your autoloader so it includes the namespace as a directory.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐