目錄
Common Use Cases for the Scripts Section
How to Define and Run Scripts
Built-in Script Events
Tips for Writing Good Scripts
首頁 開發工具 composer composer.json中的腳本部分是什麼?

composer.json中的腳本部分是什麼?

Jul 30, 2025 am 02:41 AM

composer.json中的"scripts"部分用於定義可使用Composer觸發的自定義命令,以簡化PHP項目中重複性任務的自動化。常見用例包括運行PHPUnit測試("test": "phpunit")、用PHPStan分析代碼("analyze": "phpstan analyze")、在Laravel中清除緩存("clear-cache": "php artisan config:clear && php artisan cache:clear")以及啟動開發服務器("start": "php -S localhost:8000")。要定義腳本,需在"scripts"下添加鍵值對,並通過composer run-script或簡寫形式composer run執行,同時支持傳遞參數。 Composer還支持內置事件如"pre-install-cmd"、"post-install-cmd"等,可在特定生命週期自動觸發操作。編寫良好腳本的建議包括保持簡短有意義、使用跨平台命令、組合多個命令、命名清晰並考慮添加註釋說明復雜邏輯。

The "scripts" section in a composer.json file is where you define custom commands that can be triggered using Composer. These commands are often used to automate tasks related to your PHP project — like running tests, clearing caches, or starting development servers — without having to remember long command lines.

Common Use Cases for the Scripts Section

Developers use the scripts section to simplify repetitive tasks and integrate with other tools. Here are some typical examples:

  • Running PHPUnit tests: "test": "phpunit"
  • Linting code with PHPStan: "analyze": "phpstan analyze"
  • Clearing cache in a framework like Laravel: "clear-cache": "php artisan config:clear && php artisan cache:clear"
  • Starting a local development server: "start": "php -S localhost:8000"

By defining these in composer.json , anyone working on the project can run them consistently, regardless of their environment.

How to Define and Run Scripts

Defining a script is straightforward. You add a key-value pair under the "scripts" section. The key is the name of the command, and the value is what gets executed.

For example:

 "scripts": {
    "test": "phpunit",
    "start": "php -S localhost:8000"
}

To run a script, you use the Composer CLI like this:

 composer run-script test

Or use the shorthand:

 composer run test

You can also pass arguments to your scripts if needed:

 "scripts": {
    "serve": "php -S localhost:$PORT"
}

Then run it with:

 composer run serve -- --PORT=8080

Built-in Script Events

Composer also supports built-in events that automatically trigger during certain lifecycle moments, such as:

  • "pre-install-cmd" / "post-install-cmd"
  • "pre-update-cmd" / "post-update-cmd"
  • "post-autoload-dump"

These are useful for doing things like regenerating autoload files, notifying after an install/update, or setting up environment files before dependencies are installed.

For example:

 "scripts": {
    "post-install-cmd": [
        "echo 'Project installed successfully!'"
    ],
    "post-update-cmd": [
        "php think migrate" 
    ]
}

These commands will run automatically after composer install or composer update .

Tips for Writing Good Scripts

Here are a few practical tips when working with scripts:

  • Keep them short and meaningful.
  • Use cross-platform commands if possible (especially if your team uses different OSes).
  • Chain multiple commands together when needed:
     "clear-all": "php artisan config:clear && php artisan route:clear"
  • Use descriptive names so others know what they do without guessing.
  • Consider adding documentation in comments inside composer.json if a script is complex.
  • 基本上就這些。

    以上是composer.json中的腳本部分是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP教程
1598
276
作曲家審計檢查什麼? 作曲家審計檢查什麼? Aug 04, 2025 pm 01:02 PM

Composer'sauditCommandChecksforsecurityVulnerabilitiesInphpprojectiencies byscanningthecomposer.lockfileagainstatabaseofnoishissues.1.itifendifeSIDESIFIESIFISIFIESOUTDETEDETEDETEDETEDETEDERVULNABLEDENCESICES,包括發行型,reportingingingingingsingversectiveversectionswithsevereveritywithseeverityleleleveLelele

我什麼時候應該運行作曲家轉儲-Autoload -o? 我什麼時候應該運行作曲家轉儲-Autoload -o? Aug 03, 2025 pm 04:54 PM

Runcomposerdump-autoload-owhendeployingtoproductiontooptimizeautoloadingperformancebygeneratingaclassmapandavoidingPSR-4directorylookups.2.Useitoptionallyafterinstallingnewpackagesifpreparingaproduction-readybuild,thoughit'snotrequiredsinceComposerre

如何指定軟件包名稱,描述和作者? 如何指定軟件包名稱,描述和作者? Aug 02, 2025 am 12:20 AM

在開發項目時,正確設置包名、描述和作者的方法如下:1.Node.js項目中通過npminit或yarninit設置package.json的name、description和author字段;2.Python項目使用pyproject.toml或setup.py配置name、description和authors;3.Rust項目則在Cargo.toml中定義name(即crate名)、description和authors。每種語言的配置格式不同但目的相同,均需遵循各自的標準格式並確保信息完整

如何使用作曲家使用環境變量 如何使用作曲家使用環境變量 Aug 14, 2025 pm 04:27 PM

Composerallowsenvironmentvariableinterpolationincomposer.jsonusing${VAR_NAME}syntax,butonlyinfieldslikescripts,extra,andconfig—notinrequireorautoload.2.Youcansetvariablesinlinewhenrunningcommands,suchasAPP_ENV=productioncomposerinstall,tocontrolbehav

作曲家非常慢,如何加快速度? 作曲家非常慢,如何加快速度? Aug 05, 2025 am 05:47 AM

Usealocalorigonionmirrorlikesatis,Toranproxy,overchinapackagistTorkeNetworkLatencyAndAvoidRateLimits.2.suretheTheTheTeComposoScoSercoSercoserCacheisEnabledandProperlyConfiguredBysetTingAvalIdcaceAcceAcceAcce-dir,避免 - 避免 - 毫無用處,no-cacheCacheCacheCacheCacheInciCacheInciNIND

如何將作曲家更新為最新版本? 如何將作曲家更新為最新版本? Aug 01, 2025 am 04:58 AM

toupdateCompoSertotheLateSteverion,FirstCheckYourCurrentVersionwithComposer--version; ifitshows1.x,grognToupdate.usecomposerself-updateforaglobalinstallationtoupgradetothetothelateTEST 22.xversion2.xversion2.xversion,

全球作曲家軟件包存儲在哪裡? 全球作曲家軟件包存儲在哪裡? Aug 16, 2025 am 03:50 AM

globalcomposerpackagesarestoredinthecomposerhomeDirectory,notinindividualprojects; onmacosandlinux,thisis〜/.composer/vendor,an Donwindows,是%appdata%\ Composer \ vendor,withExeCutablesInvendor/bin; 1.Thecacheislocatedat〜/.composer/cache(macOS/linux)或%ap

如何檢查作曲家是否已安裝? 如何檢查作曲家是否已安裝? Aug 03, 2025 pm 04:52 PM

Runcomposer--versionintheterminalorcommandprompt;ifComposerisinstalled,itwilldisplaytheversionnumber.2.Ifyoureceivea'commandnotfound'orsimilarerror,Composerisnotinstalledornotinthesystem’sPATH.3.Alternatively,usecomposerhelptocheckforthehelpmenuascon

See all articles