Slim Framework を使用するのはこれが初めてですが、これまでのところ、アプリケーションのルック アンド フィールがとても気に入っています。Express API サーバーのようになります。
ローカル マシンに小さなサーバーを構築しましたが、これは完全に正常に動作し、仮想ホストで動作します。
ローカルに設定した仮想ホストと関係がありますが、.htaccess ファイルでこれを回避しようとしました。
運用サーバーでは、http://www.domain.com/public/resourceと入力すると機能します。
私のディレクトリ構造は次のとおりです。
public/
index.php
.htaccess
src/
.htaccess
ルート フォルダーの .htaccess は次のようになります。
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
public フォルダーの .htaccess は次のようになります。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
また、index.php の一部を投稿し、これまでにログを記録した場所を示します。
$app = new Slim\Slim();
$env = $app->environment();
switch($env['REMOTE_ADDR']) {
case '127.0.0.1':
define('ENVIRONMENT', 'local');
break;
default:
define('ENVIRONMENT', 'production');
break;
}
// Here a log works when you go to http://www.domain.com
// Get
$app->get('/:resource(/(:id)(/))', function($resource, $id = null) {
// Here it works on local machine on production there's no possible way to get here
// var_dump('foobar');die; will respond nothing to cURL -i -X GET http://www.domain.com/resource
// The request will return a 404 error
$resource = \App\Resource::load($resource);
if ($resource === null) {
\App\Resource::response(\App\Resource::STATUS_NOT_FOUND);
} else {
$resource->get($id);
}
});