婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > Laravel第三方包報(bào)class not found的解決方法

Laravel第三方包報(bào)class not found的解決方法

熱門(mén)標(biāo)簽:電銷(xiāo)招聘機(jī)器人 事業(yè)單位如何百度地圖標(biāo)注 天津營(yíng)銷(xiāo)電話(huà)機(jī)器人加盟代理 福泉電話(huà)機(jī)器人 南寧crm外呼系統(tǒng)平臺(tái) 熱線(xiàn)電話(huà)機(jī)器人 地圖標(biāo)注入哪個(gè)科目 格陵蘭島地圖標(biāo)注 太原極信防封電銷(xiāo)卡

出現(xiàn)的問(wèn)題

公司開(kāi)發(fā)使用PHP,技術(shù)框架使用Laravel。最近線(xiàn)上出現(xiàn)一個(gè)問(wèn)題,就是上線(xiàn)之后,每次都會(huì)出錯(cuò)。查看出錯(cuò)原因,是composer安裝的第三方出現(xiàn)class not found。因?yàn)檫@個(gè)問(wèn)題,在線(xiàn)下使用Lumen框架的時(shí)候,遇到過(guò),查找問(wèn)題原因是因?yàn)橐蕾?lài)的composer包中composer.json中的”autoload”:{“psr-4”:{}}書(shū)寫(xiě)格式問(wèn)題。解決方法使用命令:composer dump-autoload -o;

雖然知道問(wèn)題的所在,但是有一個(gè)現(xiàn)象比較費(fèi)解:這個(gè)第三方包已經(jīng)使用很久了,為什么最近才開(kāi)始報(bào)錯(cuò)呢?下面就開(kāi)始查找出錯(cuò)原因

解決方案

如果確認(rèn)第三方包已安裝,并且正確使用use引用了,嘗試執(zhí)行composer dump-autoload -o

最終結(jié)果

因?yàn)榭赡芷鶗?huì)比較長(zhǎng),所以這里先說(shuō)明一下最終問(wèn)題處理結(jié)果:原因還未準(zhǔn)確定位到,現(xiàn)推測(cè)發(fā)布服務(wù)器環(huán)境問(wèn)題,但因?yàn)榘l(fā)布服務(wù)器監(jiān)控服務(wù)較多,不允許進(jìn)行測(cè)試,所以具體環(huán)境哪個(gè)配置導(dǎo)致的問(wèn)題,還沒(méi)有定位到。

下面主要介紹問(wèn)題解決過(guò)程:

 1. 查看laravel autoload
 2. 查看composer源碼;
 3. 重新編譯composer打印日志;
 4. 分析composer install過(guò)程;
 5. 查看php artisan optimize源碼

對(duì)分析查找問(wèn)題的過(guò)程感興趣的同學(xué)可以繼續(xù)往下看。

問(wèn)題分析及解決過(guò)程

1. 查找class not found原因

分析

既然class not found,確認(rèn)composer包已經(jīng)安裝。那問(wèn)題就確定在autoload過(guò)程

查看源碼

首先自動(dòng)加載入口 public/index.php 中

require __DIR__.'/../bootstrap/autoload.php';

然后繼續(xù)進(jìn)入 bootstrap/autoload.php 文件

require __DIR__.'/../vendor/autoload.php';

然后繼續(xù)進(jìn)入 vendor/autoload.php

// require 自動(dòng)加載類(lèi)
require_once __DIR__ . '/composer/autoload_real.php';

// 真正返回文件列表的操作
return ComposerAutoloaderInit3f39d071b2e74e04102a9c9b6f221123::getLoader();

進(jìn)入getLoader()方法中

public static function getLoader()
{
 if (null !== self::$loader) {
 return self::$loader;
 }

 // 注冊(cè)自動(dòng)加載方法,用來(lái)后面初始化ClassLoader類(lèi)
 spl_autoload_register(array('ComposerAutoloaderInit3f39d071b2e74e04102a9c9b6f221123', 'loadClassLoader'), true, true);
 // 初始化ClassLoarder
 self::$loader = $loader = new \Composer\Autoload\ClassLoader();
 spl_autoload_unregister(array('ComposerAutoloaderInit3f39d071b2e74e04102a9c9b6f221123', 'loadClassLoader'));

 // 這里zend_loader_file_encoded查了一下,解釋為:
 // Returns TRUE if the current file was encoded with Zend Guard or FALSE otherwise. If FALSE, consider disabling the Guard Loader
 // 又查了一下Zend Guard,貌似是php代碼加密并提高執(zhí)行效率的,提高有限,比較雞肋
 // 打印了一下,發(fā)現(xiàn)不存在這個(gè)方法,即!function_exists('zend_loader_file_encoded')為true
 $useStaticLoader = PHP_VERSION_ID >= 50600  !defined('HHVM_VERSION')  (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
 if ($useStaticLoader) {
 // 程序在這里執(zhí)行
 // 引用ComposerStaticInit類(lèi)
 require_once __DIR__ . '/autoload_static.php';

 // 調(diào)用ComposerStaticInit類(lèi)中的getInitializer方法
 // 主要作用是使用ComposerStaticInit類(lèi)中的值初始化上面創(chuàng)建的ComposerAutoloader對(duì)象中的prefixLengthsPsr4、prefixDirsPsr4、prefixesPsr0、classMap等值
 call_user_func(\Composer\Autoload\ComposerStaticInit3f39d071b2e74e04102a9c9b6f221123::getInitializer($loader));
 } else {
 $map = require __DIR__ . '/autoload_namespaces.php';
 foreach ($map as $namespace => $path) {
  $loader->set($namespace, $path);
 }

 $map = require __DIR__ . '/autoload_psr4.php';
 foreach ($map as $namespace => $path) {
  $loader->setPsr4($namespace, $path);
 }

 $classMap = require __DIR__ . '/autoload_classmap.php';
 if ($classMap) {
  $loader->addClassMap($classMap);
 }
 }

 // 重點(diǎn)在這個(gè)方法
 $loader->register(true);

 if ($useStaticLoader) {
 $includeFiles = Composer\Autoload\ComposerStaticInit3f39d071b2e74e04102a9c9b6f221123::$files;
 } else {
 $includeFiles = require __DIR__ . '/autoload_files.php';
 }
 foreach ($includeFiles as $fileIdentifier => $file) {
 composerRequire3f39d071b2e74e04102a9c9b6f221123($fileIdentifier, $file);
 }

 return $loader;
}

ClassLoader的register方法

public function register($prepend = false)
{
 // 調(diào)用ClassLoader類(lèi)的loadClass方法
 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}

ClassLoader類(lèi)的loadClass方法

public function loadClass($class)
{
 // 查找文件,如果查找到文件,則加載文件
 if ($file = $this->findFile($class)) {
 includeFile($file);

 return true;
 }
}

ClassLoader類(lèi)的findFile方法

public function findFile($class)
{
 // class map lookup
 // class map加載方式,我的理解:是通過(guò)將類(lèi)與對(duì)應(yīng)路徑生成一個(gè)對(duì)應(yīng)表
 // 該方式優(yōu)點(diǎn):加載速度快,相當(dāng)于查詢(xún)字典;
 // 缺點(diǎn):無(wú)法實(shí)現(xiàn)自動(dòng)加載,添加新類(lèi)后,需要對(duì)應(yīng)維護(hù)class map
 if (isset($this->classMap[$class])) {
 return $this->classMap[$class];
 }

 // $classMapAuthoritative默認(rèn)值為false,流程到目前,沒(méi)有設(shè)置過(guò)該值
 // $missingClasses通過(guò)查看該方法最后幾行,發(fā)現(xiàn)作用是記錄自動(dòng)加載過(guò)程中不存在的文件
 // 所以這里第一次加載會(huì)返回false
 if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
 return false;
 }

 // APCu 是老牌 PHP 字節(jié)碼和對(duì)象緩存,緩存器 APC 的分支(PS:我也是查的,不懂呀~大家感興趣可以自己深研究)
 // 經(jīng)測(cè)試,$this->apcuPrefix=null
 if (null !== $this->apcuPrefix) {
 $file = apcu_fetch($this->apcuPrefix.$class, $hit);
 if ($hit) {
  return $file;
 }
 }

 // 最后一層方法(保證是最后一個(gè)方法)
 $file = $this->findFileWithExtension($class, '.php');

 // Search for Hack files if we are running on HHVM
 if (false === $file  defined('HHVM_VERSION')) {
 $file = $this->findFileWithExtension($class, '.hh');
 }

 if (null !== $this->apcuPrefix) {
 apcu_add($this->apcuPrefix.$class, $file);
 }

 // 記錄無(wú)法找到的類(lèi),方便再次加載直接返回
 if (false === $file) {
 // Remember that this class does not exist.
 $this->missingClasses[$class] = true;
 }

 return $file;
}

ClassLoader類(lèi)中findFileWithExtension方法

private function findFileWithExtension($class, $ext)
{
 // 終于看到加載psr-4了
 // PSR-4 lookup
 // 對(duì)路徑中的\轉(zhuǎn)換為文件系統(tǒng)中對(duì)應(yīng)路徑分隔符并+后綴,
 // 比如wan\test類(lèi),最后處理為wan/test.php(linux下)
 $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;

 // 獲得類(lèi)名中第一個(gè)字母,主要用于在ClassLoader中prefixLengthsPsr4快速檢索包,并找到對(duì)應(yīng)包前綴長(zhǎng)度,后面截取時(shí)使用
 // 對(duì)比autoload_static.php中的$prefixLengthsPsr4即可明白作用
 $first = $class[0];
 if (isset($this->prefixLengthsPsr4[$first])) {
 $subPath = $class;
 while (false !== $lastPos = strrpos($subPath, '\\')) {
  // 從右往左一層層循環(huán)類(lèi)名中的路徑
  $subPath = substr($subPath, 0, $lastPos);
  $search = $subPath.'\\';
  // 找到對(duì)應(yīng)composer包前綴后,取出對(duì)應(yīng)路徑,將包前綴截取后,替換成對(duì)應(yīng)的目錄路徑,即為class所對(duì)應(yīng)文件
  if (isset($this->prefixDirsPsr4[$search])) {
  foreach ($this->prefixDirsPsr4[$search] as $dir) {
   $length = $this->prefixLengthsPsr4[$first][$search];
   if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
   return $file;
   }
  }
  }
 }
 }

 // 到這里psr-4文件就加載完了,后面是psr-0等其他文件加載,這里就不分析了。
 // 這里分析一下為什么是第三方包psr-4格式錯(cuò)誤
 // 比如包名為wan/lib,即composer安裝命令對(duì)應(yīng)composer require wan/lib
 // 第三方包中autoload psr-4配置為 "psr-4" : { "wan\\" : "src" } 
 // (**警告:上面是錯(cuò)誤配置,為了舉例說(shuō)明;正確應(yīng)該是"psr-4" : { "wan\\lib\\" : "src" })
 // 最終生成的$prefixLengthsPsr4為{'w' =>array ('wan\\' => 5,),}
 // 生成$prefixDirsPsr4為'wan\\' => array (0 => __DIR__ . '/..' . '/wan/lib/src',),
 // 對(duì)應(yīng)上面代碼,在最后$file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length)
 // $file拼接出來(lái)的路徑是vendor/wan/lib/src/lib/$className.php,導(dǎo)致最后無(wú)法拼接出正確路徑

 // PSR-4 fallback dirs
 foreach ($this->fallbackDirsPsr4 as $dir) {
 if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
  return $file;
 }
 }

 // PSR-0 lookup
 if (false !== $pos = strrpos($class, '\\')) {
 // namespaced class name
 $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
  . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
 } else {
 // PEAR-like class name
 $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
 }

 if (isset($this->prefixesPsr0[$first])) {
 foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
  if (0 === strpos($class, $prefix)) {
  foreach ($dirs as $dir) {
   if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
   return $file;
   }
  }
  }
 }
 }

 // PSR-0 fallback dirs
 foreach ($this->fallbackDirsPsr0 as $dir) {
 if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
  return $file;
 }
 }

 // PSR-0 include paths.
 if ($this->useIncludePath  $file = stream_resolve_include_path($logicalPathPsr0)) {
 return $file;
 }

 return false;
}

總結(jié)

因?yàn)椴檎疫^(guò)程比較長(zhǎng),導(dǎo)致篇幅也比較長(zhǎng)。所以決定拆分成多篇文章說(shuō)明。

到這里,通過(guò)查找問(wèn)題,把Laravel框架autoload機(jī)制源碼分析了一遍,也學(xué)會(huì)了composer包中對(duì)應(yīng)autoload信息中psr-4及classmap信息如何配置。

后續(xù)文章中會(huì)通過(guò)查看分析composer源碼及php artisan命令源碼,分析為什么本地開(kāi)發(fā)環(huán)境及測(cè)試環(huán)境沒(méi)有出現(xiàn)class not found情況

以上這篇Laravel第三方包報(bào)class not found的解決方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Laravel 加載第三方類(lèi)庫(kù)的方法
  • php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解決方法

標(biāo)簽:佳木斯 通化 香港 郴州 金華 寶雞 阿克蘇 自貢

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel第三方包報(bào)class not found的解決方法》,本文關(guān)鍵詞  Laravel,第三方,包報(bào),class,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Laravel第三方包報(bào)class not found的解決方法》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Laravel第三方包報(bào)class not found的解決方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 缙云县| 荃湾区| 阿拉善盟| 毕节市| 迁安市| 西昌市| 饶河县| 丰顺县| 宜章县| 永川市| 浦城县| 哈密市| 鄂州市| 丰原市| 达拉特旗| 莱西市| 三门县| 荥阳市| 洞头县| 富蕴县| 宁河县| 金阳县| 汤阴县| 岱山县| 临海市| 万全县| 西林县| 印江| 沙坪坝区| 台南市| 张掖市| 抚州市| 广丰县| 遂平县| 手游| 茂名市| 湖口县| 娄底市| 龙井市| 会理县| 万年县|