LoginSignup
7
8

More than 5 years have passed since last update.

Slim 3 PHP Framework をレンタルサーバーで使用するための修正

Posted at

最近、導入の手軽さと学習コストの低さから Slim 3 PHP Framework をよく使っています。

ただ、Slim 3 (というか大抵のフレームワーク) は公開フォルダがプロジェクトルートのいっこ下のディレクトリなので、レンタルサーバーでは使いにくいと感じます。

とはいえ、フレームワーク内のファイルをちょろっと書き換えるだけで使えるようになります。

簡単なので、ブログに書くほどのことでも無いかと思ったのですが、これから先、毎度探すことになるのは面倒なので備忘録として残しておきます。

RewriteEngine On

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /
RewriteCond %{REQUEST_URI} !(^/public/)
RewriteRule ^ index.php [QSA,L]
<?php
if (PHP_SAPI == 'cli-server') {
    // To help the built-in PHP dev server, check if the request was actually for
    // something which should probably be served as a static file
    $url  = parse_url($_SERVER['REQUEST_URI']);
    $file = __DIR__ . $url['path'];
    if (is_file($file)) {
        return false;
    }
}

require __DIR__ . '/vendor/autoload.php';

session_start();

// Instantiate the app
$settings = require __DIR__ . '/src/settings.php';
$app = new \Slim\App($settings);

// Set up dependencies
require __DIR__ . '/src/dependencies.php';

// Register middleware
require __DIR__ . '/src/middleware.php';

// Register routes
require __DIR__ . '/src/routes.php';

// Run app
$app->run();

.htaccess は script や stylesheet があるディレクトリへのアクセスはそのまま通して、それ以外は URL を書き換えるという感じ。

index.php はもともと public にあったものをプロジェクトのルートに置き、各ファイルを読みに行くためのパスを変更しました。

これでレンタルサーバーでさらっと使えるようになります。

7
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8