laravel 如何使用composer自動加載本身定義的文件夾

原文地址laravel

一. 問題json

當咱們 clone下來一個laravel框架,接着就開始咱們表演,可是咱們根據業務需求須要建立一些自定義的文件夾,那麼咱們該如何加載他們呢,如何避免這類錯誤[Symfony\\Component\\Debug\\Exception\\FatalThrowableError] Class 'tools\\alyduanxin\\api\_demo\\SmsDemo' not foundapi

二:分三步來解決這個問題app

  • 在laravel 中項目根目錄下建立本身的文件夾,例如我在項目根目錄下建立了一個tools文件夾。
  • 在項目文件夾的根目錄下找到composer.json文件,在autoload裏添加psr-4節點composer

    "psr-4": {
                "tools\\": "tools/"
            }
  • 接着執行框架

    composer dump-autoload -o
  • 至此咱們就加載成功啦

三. 解釋一下 composer dump-autoload 這條命令less

下面是composer官方文檔中關於dump-autoload命令的解釋:性能

dump-autoload優化

If you need to update the autoloader(類加載器) because of new classes in a classmap package for example, you can use "dump-autoload" to do that without having to go through an install or update.
Additionally, it can dump an optimized(優化) autoloader that converts(轉化) PSR-0/4 packages into classmap ones for performance(性能) reasons. In large applications with many classes, the autoloader can take up a substantial portion of every request's time. Using classmaps for everything is less convenient in development, but using this option you can still use PSR-0/4 for convenience and classmaps for performance.
Options:
--no-scripts: Skips the execution of all scripts defined in composer.json file.
--optimize (-o): Convert PSR-0/4 autoloading to classmap to get a faster autoloader. This is recommended especially for production, but can take a bit of time to run so it is currently not done by default.
--classmap-authoritative (-a): Autoload classes from the classmap only. Implicitly enables --optimize.
--apcu: Use APCu to cache found/not-found classes.
--no-dev: Disables autoload-dev rules.
其中官方推薦的參數是 -o,即 composer dump-autoload -o
這個命令能夠將PSR-0/4自動加載成classmap來獲取一個更快速的類加載器,推薦生產環境使用,可是,可能須要一點時間來運行,所以目前不是默認this

相關文章
相關標籤/搜索