在子目录中托管Slim微型网站

问题描述 投票:1回答:2

是否有一个很好的和支持的方式来定义这样的路线:

$app->get('/', function (Request $request, Response $response) {
});
$app->get('/hello/{name}', function (Request $request, Response $response) {
});

...无论index.php路由文件的位置如何,让它们按预期工作?

例如。如果DOCUMENT_ROOTC:\Projects\Playgroundhttp://playground.example.com),我的路由器文件位于C:\Projects\Playground\PHP\Slim\foo\bar\index.phphttp://playground.example.com/PHP/Slim/foo/bar)我想http://playground.example.com/PHP/Slim/foo/bar/hello/world!匹配'/hello/{name}'

(属于Slim的每个其他文件都在其他地方,让我们说C:\Libraries\Slim,并且应该与这个问题无关。)

很好,支持我的意思不是这个:

$uri_prefix = complex_function_to_calculate_it($_SERVER['DOCUMENT_ROOT'], __DIR__);
$app->get($uri_prefix . '/hello/{name}', function (Request $request, Response $response) {
});

Slim 3使用nikic/fast-route,但我找不到任何提示。

php slim
2个回答
0
投票

哦好吧......如果不支持开箱即用,我想最好不要过多地使它复杂化并使其成为一个参数:

define('ROUTE_PREFIX', '/PHP/Slim/foo/bar');
$app->get(ROUTE_PREFIX . '/hello/{name}', function (Request $request, Response $response) {
});

毕竟,编写在所有情况下动态计算它的通用代码是一个很大的负担,尤其是。当你可以有无限的因素,如符号链接或Apache别名。

该参数当然应该与您的其他网站设置一起定义(如果您使用src/settings.php则为Slim Skeleton,如果您使用.env则为phpdotenv ......无论如何)。


0
投票

除了在子目录中创建.htaccess文件:

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

您需要配置Web服务器或苗条:

解决方案1(配置apache):

在debian 9上打开以下文件:

/etc/apache2/apache2.conf

一旦打开,修改

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

然后重新启动apache以应用更改:

systemctl restart apache2

解决方案2(配置超薄):

附上这个:

// Activating routes in a subfolder
$container['environment'] = function () {
    $scriptName = $_SERVER['SCRIPT_NAME'];
    $_SERVER['SCRIPT_NAME'] = dirname(dirname($scriptName)) . '/' . basename($scriptName);
    return new Slim\Http\Environment($_SERVER);
};

请注意,您需要在路线中使用subdir name,例如/ subdir / for home,或某些特定路由:/ subdir / auth / signup

© www.soinside.com 2019 - 2024. All rights reserved.