Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Slimをオススメしてみる ディレクトリ構造

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for gallu gallu
October 25, 2023

Slimをオススメしてみる ディレクトリ構造

Avatar for gallu

gallu

October 25, 2023
Tweet

More Decks by gallu

Other Decks in Technology

Transcript

  1. Slimいいよ!!  https://www.slimframework.com/  Slim is a PHP micro framework

    that helps you quickly write simple yet powerful web applications and APIs.  Slim は、シンプルかつ強力な Web アプリケーションと API をすばやく作 成するのに役立つ PHP マイクロ フレームワークです。(Google)  Slimは、シンプルかつパワフルなWebアプリケーションやAPIを素早く作 成できるPHPマイクロフレームワークです。(DeepL)  Slimいいです!!  micro framework いいです!!  シンプルでいいです!!  っていう個人の感想と妄想を垂れ流してみます(笑
  2. Laravelのディレクトリ構成  いわゆる「MVC」ベースのディレク トリ構成ですね  ぱっと見で大体分かる……ような 気がするんですがどうなんですか ねぇ?  実際「わかりやすい」んじゃない、

    かなぁ? と。多分  昔は Modelsが無かった記憶がご ざいます(うろ覚え app/ Console/ Exceptions/ Http/ Controllers/ Middleware/ Jobs/ Models/ Providers/ Rules/ bootstrap/ config/ database/ public/ resources/ routes/ storage/ logs/ tests/
  3. CakePHPのディレクトリ構成  こちらも「MVC」ベースです  うっすらとLaravelとの違いはあるものの、 やっぱり大体「見ればあらかた分かる」 感じですね  Laravelの app/

    に対して、CakePHPの ほうは src/ って感じが興味深いです bin/ config/ plugins/ resources/ src/ Console/ Controller/ Model/ View/ templates/ tests/ tmp/ webroot/
  4. Slim……はまず素のinstallをおさらい # まずは必要なものをinstall composer require slim/slim composer require slim/http composer

    require slim/psr7 # こんな感じになります [gallu@鯖 slim_plain]$ ls composer.json composer.lock vendor # いわゆる「DocumentRoot」を作成します mkdir public vi public/index.php (コードは後述) # 簡易サーバ起動して動く事を確認 php -S 0.0.0.0:8080 -t public/
  5. 設置したindex.php <?php use Psr¥Http¥Message¥ResponseInterface as Response; use Psr¥Http¥Message¥ServerRequestInterface as Request;

    use Slim¥Factory¥AppFactory; require __DIR__ . '/../vendor/autoload.php'; // Instantiate App $app = AppFactory::create(); // Add error middleware $app->addErrorMiddleware(true, true, true); // Add routes $app->get('/', function (Request $request, Response $response) { $response->getBody()->write("Hello"); return $response; }); $app->run();
  6. (結果)素のSlimのディレクトリ構成  シンプル!  侘び寂び!!  後はどうとでも如何様にでも!!!  あなたの設計とコードに合わせて自由に!!!! 

    真っ白なカンバスに好きな絵を描くことができます!!!!!  無限の荒野が広がってます!!!!!!  スバラシイと思いませんか?  とはいえまぁ流石にこれは「荒野に過ぎる」ので…… public/
  7. バージョン3の頃  https://github.com/slimphp/Slim-Skeleton/tree/3.1.8  注目は「srcに設定ファイル」?  あとはまぁわかりやすく  プログラムは別途 app/

    を切る事が多いかと思います  autoloaderの設定を忘れずに  その辺はあくまでも「自由に」 src/ 設定ファイル routes.php settings.php middleware.php dependencies.php public/ templates/ logs/ tests/Functional/
  8. バージョン4の頃  https://github.com/slimphp/Slim-Skeleton/tree/4.5.0  少しクリーンアークテクチャぽいのを意識してる?  DomainとかInfrastructureとか  突っ込みどころはまぁ…… 

    リポジトリだけ、なぜ app/ ?  srcの名前空間名…… app/ 設定ファイル routes.php settings.php middleware.php dependencies.php repositories.php src/ (namespace App¥...) Application/ Domain/ Infrastructure/Persistence/User/ public/ templates/ logs/ tests/
  9. まとめ  もちろん他のフレームワークでも「ディレクトリ構成をごっ そりと入れ替える」事は大体可能だと思います  知ってる限りでは「出来ない事」はそれほど無かった記憶  ただ「それほど」なので、シンドイ箇所が無いわけでもなく  一方でSlimは「そもそもディレクトリ構成とか存在しなかっ

    た」ベースなので、思うがままに自由に設計できます(笑  まぁ結果「大体Laravelに酷似」とかよくありますが(爆  とはいえ案件によっては「妙な縛りがあってそれがフレー ムワークの縛りと相性が悪い」なんてことも  そんな時「にこそ」、選択肢としてSlimを、是非!!!(笑