自作のcomposerパッケージ(hello,world)を、githubに登録して、laravelから利用できる方法
composerで色々とインストールしていると、これってどうやって実現しているのか?と気になったので、自分でも試してみた。
1, まずディレクトリ作成して、その中でcomposer initでcomposer.jsonを作る。
※当然ながら、composerインストールが必須。
2, 対話形式で色々と聞いてくる。composer.jsonはテキストファイルなので後で修正できるので気楽に書く(一度削除して、最初からやり直しても良い)
※ composer validateで、正しい形式のcomposer.jsonかチェックしてくれるので、テキストエディタで修正したらチェックする事!
| 1 2 3 4 5 6 7 8 9 | # (必須)githubのアカウント名/githubのrepository名、を入力する Package name (<vendor>/<name>) [windowsのユーザ名/フォルダ名]: fddcddhdd/helloworld # (必須)パッケージの説明を書く Description []: test for composer # 作者情報, nで記入しない!(しても別にいい) Author [, n to skip]: n # (推奨) とりあえずMITライセンスで License []: MIT # 後はEnter連打する | 
3, ディレクトリ内に、パッケージ名・説明・ライセンスしかない最もシンプルなcomposer.jsonが生成される。
autoloadだけテキストエディタで追加する(なぜかCLIから記述出来ない)
これを記述しないと
use fddcddhdd/helloworld で使えずに
require_once “src/fddcddhdd/helloworld/Test.php” と書かないと使えない
| 1 2 3 4 5 6 7 8 9 10 11 | {     "name": "fddcddhdd/helloworld",     "description": "test for composer",     "license": "MIT",     "require": {},     "autoload": {         "psr-4": {             "fddcddhdd\\helloworld\\": "src/"         }     } } | 
4, hello,worldと表示するphpファイル&フォルダを作る
フォルダ/src/fddcddhdd/helloworld/Test.php
| 1 2 3 4 5 6 7 8 9 10 | <?php namespace fddcddhdd; class helloworld {     public function say()     {         return 'hello, world';     } } | 
5, githubにてレポジトリhelloworldを生成。ローカルレポジトリをpushする。
https://github.com にして新規レポジトリを生成(README.mdは作成チェックはしない!)すると、以下のコマンドがブラウザに表示されるので、
最初に作成したフォルダまでターミナルで行って入力する
| 1 2 3 4 5 6 7 8 | echo "# helloworld" >> README.md git init git add README.md git commit -m "first commit" # git remote add origin https://github.com/ユーザ名/レポジトリ名.git git remote add origin https://github.com/fddcddhdd/helloworld.git git push -u origin master | 
6, Packagist(composerのメインリポジトリ・サイト)に登録する。
githubアカウントでソーシャル・ログイン出来るかと思いきや、メアドとパスワード入力が必要(chromeの自動生成パスワードにした)
上部ナビバーのSubmitをクリックすると、入力欄が出てくるのでgithubのレポジトリURLをコピペして登録する。
https://github.com/fddcddhdd/helloworld
「同じ名前のパッケージが100件近くあるけど、本当に登録するの?」と聞かれるけど、迷わずsubmit!
Similarly named packages:
abbert/helloworld
aj/helloworld
amir/helloworld
amircodiant/helloworld
apoca/helloworld
And 81 more
逆に言えば、全世界中で使われているcomposerなのに、HelloWorldプログラムが86個しか登録されていないのか…。
7, 問題なければ、composerからインストールできるようになるはずなんで、laravelプロジェクトフォルダに移動してコマンドを叩く
| 1 2 3 4 5 6 7 8 9 | composer require fddcddhdd/helloworld Using version dev-master for fddcddhdd/helloworld ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals   - Installing fddcddhdd/helloworld (dev-master 257daa3): Cloning 257daa3eaa from cache Writing lock file Generating optimized autoload files | 
プロジェクトフォルダ\vendor\fddcddhdd\helloworld\src\fddcddhdd\helloworld\Test.phpがダウンロードされた事を確認。
8, laravelから使ってみる。
| 1 2 3 4 5 6 7 8 9 10 | use fddcddhdd\helloworld; class HomeController extends Controller {     public function index()     {         $helloworld = new helloworld;         return $helloworld->say();     } } | 
参考URL:
https://blog.monochromegane.com/blog/2013/06/02/development-php-composer-library-at-packagist-1/