📝

Slim 3 Framework いまさらVagrant環境にインストールしてみる。その1

2021/04/05に公開

さくっとフレームワークを使うときslimがいいらしい

php でとりあえずさくっとプログラムを作りたいがスクラッチだとなー、でも軽量なフレームワークなんかないか~なと探していたらslimがいいよっていうのを聞いたのでインストールしてみた。

Slim 3 Frameworkリリースされたので、ざっと紹介

php は、5.5以上を使うとのこと

php 5.3でいいやと思っていたらサポート期間切れているのね知らなかった。いまどきだと 5.6 or 7.0 がいいみたいですな。

インストール

php 7.0 を手順に従ってインストールしてみる。MySQLのアクセスしたいのでpdoもいれてみる(もしかしたらフレームワークで用意されているのかもしれないけど…)。Apache2 もインストールされる。

# yum install epel-release
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# yum install --enablerepo=remi,remi-php70 php php-devel php-mbstring php-pdo php-gd php-mysqlnd unzip

composer もインストール

# curl -s https://getcomposer.org/installer | php
# mv composer.phar /usr/local/bin/composer

早速 slim をインスコ

本家の User Guide Installation 手順に従ってやってみる。

$ mkdir slim; cd slim
$ composer require slim/slim "^3.0"

./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing container-interop/container-interop (1.1.0)
    Downloading: 100%         

  - Installing nikic/fast-route (v1.0.1)
    Downloading: 100%         

  - Installing psr/http-message (1.0.1)
    Downloading: 100%         

  - Installing pimple/pimple (v3.0.2)
    Downloading: 100%         

  - Installing slim/slim (3.5.0)
    Downloading: 100%         

Writing lock file
Generating autoload files

とりあえずslim動作確認

$ cp ./vendor/slim/slim/example/index.php .
$ php -S localhost:8000 -t .

下記のコマンドを実行して動作確認する。

$ curl http://localhost:8000
$ curl http://localhost:8000/hello
$ curl http://localhost:8000/hello/hage

Web Service化

本家の User Guide Web Service 手順に従ってやってみる。apache のバーチャルホスト設定共にやってみる。

まずslimの移動

# mv /home/vagrant/slim /var/www
# mkdir -p /var/www/slim/public
# cp -p /var/www/slim/vendor/slim/slim/example/index.php /var/www/slim/public/

apache のバーチャルホスト設定 slim3.conf つーファイルで作ってみたりする。

Listen 8080
NameVirtualHost *:8080
<VirtualHost *:8080>
  ServerName slim3
  DocumentRoot /var/www/slim/public
  ErrorLog /var/log/httpd/slim.error.log
  CustomLog /var/log/httpd/slim.access.log combined
  LogLevel warn
  <Directory /var/www/slim/public>
    AllowOverride All
  </Directory>
</VirtualHost>

.htaccess も設定するってことで。こちらは ./vendor/slim/slim/example/.htaccess を参考にするとよいかも。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

apache を再起動して確認。

終わり、これだけで使えるのかー。しかもslimのサイトシンプルでいいわ。

次回、MySQLアクセスしたデータの表示とかやってみる。

GitHubで編集を提案

Discussion