LoginSignup
1

More than 5 years have passed since last update.

MacにComposerをインストールし、guzzlehttpを入れてみた

Posted at

既存のPHPコードを保守するためにgitからローカルにコードを落としてきたが、
普段使っているローカルPCで一度もPHPを触ったことが無く、ライブラリが全く入っていなかった。

今回はMacにComposerをインストールして、ライブラリを一つ入れるところまでを記録に残しておく。
ちなみに普段PHPを触る機会はほぼ0なので、Composerも初めて触るPHP初心者。

環境

OS バージョン
macOS High Serria 10.13.5

手順

Composerインストール

インストール
$ cd ~/
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
$ composer -V

Composer設定

入力する内容は以下2項目。入力値は適宜変更する。
他はenterを押すのみ。

項目 入力値
description xxxxxx
license proprietary
設定
$ composer init

  Welcome to the Composer config generator

This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [xxxxxx/xxxxxx]:
Description []: xxxxxx
Author [xxxxxx <xxxxxx@xxxxxx.Xxxxxx>, n to skip]: 
Minimum Stability []:
Package Type (e.g. library, project, metapackage, composer-plugin) []:
License []: proprietary

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]?
Search for a package:
Would you like to define your dev dependencies (require-dev) interactively [yes]?
Search for a package:

{
    "name": "xxxxxx/xxxxxx",
    "description": "xxxxxx",
    "license": "proprietary",
    "authors": [
        {
            "name": "xxxxxx",
            "email": "xxxxxx@xxxxxx.Xxxxxx"
        }
    ],
    "require": {}
}

Do you confirm generation [yes]?

これで.composerディレクトリ、composer.jsonが生成される。
composer.jsonの中身は先ほど入力した内容が反映されている。

composer.json
{
    "name": "xxxxxx/xxxxxx",
    "description": "xxxxxx",
    "license": "proprietary",
    "authors": [
        {
            "name": "xxxxxx",
            "email": "xxxxxx@xxxxxx.Xxxxxx"
        }
    ]
}

composer.jsonを.composerの下へmvし、今回はguzzlehttpを入れてみる。

ライブラリ追加
$ mv ./composer.json .composer
$ cd .composer
$ composer require guzzlehttp/guzzle

composer.jsonを見ると追加したライブラリが反映されている。

composer.json
{
    "name": "xxxxxx/xxxxxx",
    "description": "xxxxxx",
    "license": "proprietary",
    "authors": [
        {
            "name": "xxxxxx",
            "email": "xxxxxx@xxxxxx.Xxxxxx"
        }
    ],
    "require": {
        "guzzlehttp/guzzle": "^6.3"
    }
}

この後composer.jsonをチェックして、「is valid」と出ればOK。

$ composer validate
./composer.json is valid

guzzlehttpが.composer/vendorの下に展開されている。

$ ls vendor/
autoload.php composer     guzzlehttp   psr

手順はここまで。

補足

PHPエディタって何が良いか周りに聞いてみたが人それぞれだった。
自分の場合はPCにeclipseが入っていたので、そのまま使ってみた。
eclipseにプロジェクトをインポートしたところ、
ライブラリパスが通っていないエラーが表示されたので今回の手順を行った。
実施後はエラーが消えたのでおそらく出来ているはず。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1