Laravelのプロジェクトを作ってみる。

  • 環境
    • macOS Mojave version10.14.3
    • PHP 7.3.1
    • Composer version 1.8.0

Laravelは、ただで使えるMVCのWeb開発用フレームワークです。

laravel.com

Laravelをインストーラを使わないでインストールします。

readouble.com

Composer Create-Projectでインストールします。

$ composer create-project --prefer-dist laravel/laravel {プロジェクトの名前}
コマンドの説明です。

利用可能なリポジトリにある既存のパッケージを複製して自身のパッケージにしたい(パッケージをフォークしたい)
$ composer create-project [vendor]/[package]

qiita.com

prefer-distオプション
GitHubなどでリポジトリを配信している場合、git cloneでソースを落としてくる(prefer-source)か、zipでダウンロードする(prefer-dist)か選ぶことができます。
通常、prefer-distでダウンロードしたほうが高速です。ただ、アクセストークンなどの認証情報をセットしておく必要があるので、設定できてない人はうまく動かないかもしれません。
そのためか、composerはprivateリポジトリの場合、デフォルトでgit cloneしようとします。
この挙動を変更してzipダウンロードを強制するのが --prefer-dist オプションで、 composer install --prefer-dist などのように使います。
blog.tojiru.net

$ composer create-project --prefer-dist laravel/laravel tryPhp
Installing laravel/laravel (v5.7.19)
  - Installing laravel/laravel (v5.7.19): Loading from cache
Created project in tryPhp
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 86 installs, 0 updates, 0 removals
  - Installing symfony/polyfill-ctype (v1.10.0): Loading from cache
  - Installing vlucas/phpdotenv (v2.6.1): Loading from cache
  - Installing symfony/css-selector (v4.2.3): Loading from cache

# 省略

laravel/framework suggests installing pusher/pusher-php-server (Required to use the Pusher broadcast driver (^3.0).)
laravel/framework suggests installing symfony/dom-crawler (Required to use most of the crawler integration testing tools (^4.1).)
laravel/framework suggests installing symfony/psr-http-message-bridge (Required to psr7 bridging features (^1.0).)
lcobucci/jwt suggests installing mdanter/ecc (Required to use Elliptic Curves based algorithms.)
psy/psysh suggests installing ext-pdo-sqlite (The doc command requires SQLite to work.)
psy/psysh suggests installing hoa/console (A pure PHP readline implementation. You'll want this if your PHP install doesn't already support readline or libedit.)
filp/whoops suggests installing whoops/soap (Formats errors as SOAP responses)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/php-code-coverage suggests installing ext-xdebug (^2.6.0)
phpunit/phpunit suggests installing phpunit/php-invoker (^2.0)
phpunit/phpunit suggests installing ext-xdebug (*)
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: beyondcode/laravel-dump-server
Discovered Package: fideloper/proxy
Discovered Package: laravel/nexmo-notification-channel
Discovered Package: laravel/slack-notification-channel
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
> @php artisan key:generate --ansi
Application key set successfully.

# バージョン確認します.
$ php artisan -V
Laravel Framework 5.7.26

# 作成されたLaravelプロジェクトのディレクトリ構成を見てみます.
$ find tryPhp/ -type d
tryPhp/
tryPhp//database  # データベースのマイグレーションとモデルファクトリ、初期値設定(シーディング)が格納される
tryPhp//database/migrations
tryPhp//database/seeds
tryPhp//database/factories
tryPhp//bootstrap  # フレームワークの初期処理を行うapp.phpファイルが格納される
tryPhp//bootstrap/cache  # 初期処理のパフォーマンスを最適化するため、フレームワークが生成するルートやサービスのキャッシュファイルが保存される
tryPhp//app   # アプリケーションのコアコードを格納するディレクトリ
tryPhp//app/Providers
tryPhp//app/Exceptions
tryPhp//app/Http # コントローラ、ミドルウェア、フォームリクエストを設置します。アプリケーションへのリクエストを処理するロジックは、ほぼ全てこのディレクトリ内に設置します。
tryPhp//app/Http/Middleware
tryPhp//app/Http/Controllers
tryPhp//app/Http/Controllers/Auth
tryPhp//app/Console # アプリケーションの全カスタムArtisanコマンドで構成します。これらのコマンドクラスはmake:commandコマンドにより生成されます。
tryPhp//config    # アプリケーションの全設定ファイルが格納される
tryPhp//resources   # ビューやアセットの元ファイル(LESS、SASS、JavaScript)で構成され、すべての言語ファイルも格納される
tryPhp//resources/js
tryPhp//resources/js/components
tryPhp//resources/lang
tryPhp//resources/lang/en
tryPhp//resources/sass
tryPhp//resources/views
tryPhp//tests
tryPhp//tests/Unit
tryPhp//tests/Feature
tryPhp//storage  # コンパイルされたBladeテンプレート、ファイルベースのセッション、ファイルキャッシュなど、フレームワークにより生成されるファイルが保存される
tryPhp//storage/app  # アプリケーションにより生成されるファイルを保存するために利用
tryPhp//storage/app/public  # プロファイルのアバターなどのようなユーザーにより生成され、外部からアクセスされるファイルが保存される
tryPhp//storage/framework  # フレームワークが生成するファイルやキャッシュに利用
tryPhp//storage/framework/cache
tryPhp//storage/framework/cache/data
tryPhp//storage/framework/testing
tryPhp//storage/framework/sessions
tryPhp//storage/framework/views
tryPhp//storage/logs   # アプリケーションのログファイルが保存される
tryPhp//public   # アプリケーションへの全リクエストの入り口となり、オートローディングを設定するindex.phpファイルが格納される
tryPhp//public/svg
tryPhp//public/css
tryPhp//public/js
tryPhp//routes
tryPhp//vendor  # Composerによる依存パッケージが配置される
# 省略

readouble.com

動かして画面を見てみます。

# 開発サーバをhttp://localhost:8000として起動します。
$ php artisan serve
Laravel development server started: <http://127.0.0.1:8000>

http://localhost:8000/にブラウザでアクセスするとLaravelの画面が表示されました。
f:id:ponsuke_tarou:20190219230855p:plain

PHPにはビルトインウェブサーバーというテスト用の開発サーバがくっついています。
# php -Sのコマンドでもphp artisan serveと同じように動作します。
$ php -S localhost:8000 -t public
PHP 7.3.1 Development Server started at Tue Feb 19 23:12:16 2019
Listening on http://localhost:8000
Document root is /Users/mana/Dropbox/ApacheDcumentRoot/tryPhp/public
Press Ctrl-C to quit.

ディレクトパーミッションを設定します。

Laravelをインストールした後に、多少のパーミッションの設定が必要です。storage下とbootstrap/cacheディレクトリをWebサーバから書き込み可能にしてください。設定しないとLaravelは正しく実行されません。
インストール 5.7 Laravel

$ cd tryPhp/

# 権限を確認します
$ ls -l | grep storage
drwxr-xr-x@  5 mana  staff     160 12 15 23:37 storage

$ ls -l bootstrap/
total 8
-rw-r--r--@ 1 mana  staff  1620 12 15 23:37 app.php
drwxr-xr-x@ 5 mana  staff   160  2 19 22:24 cache

# 書き込み権限を追加する
$ chmod o+w storage/
$ chmod o+w bootstrap/cache/

# 権限を確認します
$ ls -l | grep storage
drwxr-xrwx@  5 mana  staff     160 12 15 23:37 storage

$ ls -l bootstrap/
total 8
-rw-r--r--@ 1 mana  staff  1620 12 15 23:37 app.php
drwxr-xrwx@ 5 mana  staff   160  2 19 22:24 cache

www.atmarkit.co.jp

f:id:ponsuke_tarou:20190219234237j:plain
思い出の一枚