Site icon PlusMagi's Blog By Pitt Phunsanit

สร้าง project ใหม่โดยใช้ composer

เดี่ยวนี้ framework ต่างๆก็ใช้ package manager อย่าง composer ติดตั้ง library ต่างๆ รวมทั้งติดตั้ง framework อื่นๆ อย่าง jQuery ถ้าต้องการที่จะสร้างโครงการของเราเองก็ทำได้ง่ายๆ

  1. สร้าง folder ใหม่ขึ้นมา เช่น C:\xampp\htdocs\snippets
  2. พิมพ์
    composer init
  3. ตอบคำถามไปตามละดับ เช่น
    D:\>cd D:\xampp\htdocs\snippets
    
    D:\xampp\htdocs\snippets>composer init
    
    
      Welcome to the Composer config generator
    
    
    
    This command will guide you through creating your composer.json config.
    
    Package name (<vendor>/<name>) [pitt.p/snippets]: pitt/snippets
    Description []: code snippets
    Author [, n to skip]: Pitt Phunsanit <phunsanit@hotmail.com>
    Minimum Stability []: stable
    Package Type (e.g. library, project, metapackage, composer-plugin) []: project
    License []: mit
    
    Define your dependencies.
    
    Would you like to define your dependencies (require) interactively [yes]? yes
    Search for a package: jquery
    
    Found 15 packages matching jquery
    
       [0] components/jquery
       [1] frameworks/jquery
       [2] contao-components/jquery
       [3] yiisoft/jquery
       [4] etdsolutions/jquery
       [5] ovidentia/jquery
       [6] mygento/jquery
       [7] quickapps-plugins/jquery
       [8] intelogie/jquery
       [9] apnet/jquery
      [10] jquery/jquery
      [11] yepsua/jquery4php
      [12] venne/jquery2.0-module
      [13] sonata-project/jquery-bundle
      [14] blueimp/jquery-file-upload
    
    Enter package # to add, or the complete package name if it is not listed: 0
    Enter the version constraint to require (or leave blank to use the latest version):
    
  4. ตอบคำถามไปเรื่อยๆ รูปแบบ packages จะสัมพันกับ url ใน github เช่น https://github.com/jquery/jquery ถ้าไม่ทราบหาได้ขากเว้บ packagist.org จนสุดท้ายจะสร้างไฟล์ composer.json ขึ้นมา
    {
        "authors": [{
            "email": "phunsanit@hotmail.com",
            "homepage": "https://pitt.plusmagi.com",
            "name": "pitt phunsanit",
            "role": "Developer"
        }],
        "description": "snippet",
        "homepage": "https://pitt.plusmagi.com",
        "license": "mit",
        "minimum-stability": "stable",
        "name": "pitt/snippet",
        "require": {
            "ckeditor/ckeditor": "*",
            "components/jquery": "*",
            "components/jquery-migrate": "*",
            "components/jqueryui": "*",
            "phpoffice/phpexcel": "^1.8",
            "twbs/bootstrap": "*"
        },
        "type": "project"
    }
  5. โหลด library โดยคำสั่ง
    composer update

ไฟล์จุถูกเก็บในโฟลเดอร์ vendor/xxx/yyy อย่างเรียบร้อยและเหมือนกันทุกเครื่องถ้าลง package เดียวกัน version เดียวกัน

Exit mobile version