在子目录中托管 Slim 微型网站
Host a Slim microsite in a subdirectory
是否有一个很好的和受支持的方式来定义这样的路由:
$app->get('/', function (Request $request, Response $response) {
});
$app->get('/hello/{name}', function (Request $request, Response $response) {
});
...无论 index.php
路由文件的位置如何,都能让它们按预期工作?
例如如果 DOCUMENT_ROOT
是 C:\Projects\Playground
(http://playground.example.com
) 并且我的路由器文件位于 C:\Projects\Playground\PHP\Slim\foo\bar\index.php
(http://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 但我找不到任何提示。
哦好吧...如果开箱即用不支持,我想最好不要把它复杂化太多并将其作为参数:
define('ROUTE_PREFIX', '/PHP/Slim/foo/bar');
$app->get(ROUTE_PREFIX . '/hello/{name}', function (Request $request, Response $response) {
});
毕竟,编写在所有情况下都动态计算它的通用代码是一个太大的负担,尤其是。当您可以拥有无穷无尽的因素(如符号链接或 Apache 别名)时。
该参数当然应该与您的站点设置的其余部分一起定义(如果您使用 Slim Skeleton, .env
if you use phpdotenv,则 src/settings.php
... 随便什么)。
除了在子目录中创建 .htaccess 文件外:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
您需要配置 web 服务器或 slim:
解决方案一(配置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
方案二(配置slim):
附上这个:
// 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/ 用于家庭或某些特定路线:/subdir/auth/signup
您可以将所有路由嵌套在 Slim 中的包装器组中,其路径模式是根据 $_SERVER
变量确定的:
$app = Slim\Factory\AppFactory::create();
$app->group(dirname($_SERVER['SCRIPT_NAME']), function($subdirectory) {
$subdirectory->group('/api/v1', $function($api) {
// define routes as usual, e.g...
$api->get('/foo/{foo:[0-9]+}', function($req, $res) {
// ...
});
});
});
对 $_SERVER['SCRIPT_NAME']
变量进行一些预处理可以让您做其他事情——检测您的 API 是否嵌套在名为 API 的目录中并且不要附加另一个API 到路由模式,例如。
是否有一个很好的和受支持的方式来定义这样的路由:
$app->get('/', function (Request $request, Response $response) {
});
$app->get('/hello/{name}', function (Request $request, Response $response) {
});
...无论 index.php
路由文件的位置如何,都能让它们按预期工作?
例如如果 DOCUMENT_ROOT
是 C:\Projects\Playground
(http://playground.example.com
) 并且我的路由器文件位于 C:\Projects\Playground\PHP\Slim\foo\bar\index.php
(http://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 但我找不到任何提示。
哦好吧...如果开箱即用不支持,我想最好不要把它复杂化太多并将其作为参数:
define('ROUTE_PREFIX', '/PHP/Slim/foo/bar');
$app->get(ROUTE_PREFIX . '/hello/{name}', function (Request $request, Response $response) {
});
毕竟,编写在所有情况下都动态计算它的通用代码是一个太大的负担,尤其是。当您可以拥有无穷无尽的因素(如符号链接或 Apache 别名)时。
该参数当然应该与您的站点设置的其余部分一起定义(如果您使用 Slim Skeleton, .env
if you use phpdotenv,则 src/settings.php
... 随便什么)。
除了在子目录中创建 .htaccess 文件外:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
您需要配置 web 服务器或 slim:
解决方案一(配置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
方案二(配置slim):
附上这个:
// 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/ 用于家庭或某些特定路线:/subdir/auth/signup
您可以将所有路由嵌套在 Slim 中的包装器组中,其路径模式是根据 $_SERVER
变量确定的:
$app = Slim\Factory\AppFactory::create();
$app->group(dirname($_SERVER['SCRIPT_NAME']), function($subdirectory) {
$subdirectory->group('/api/v1', $function($api) {
// define routes as usual, e.g...
$api->get('/foo/{foo:[0-9]+}', function($req, $res) {
// ...
});
});
});
对 $_SERVER['SCRIPT_NAME']
变量进行一些预处理可以让您做其他事情——检测您的 API 是否嵌套在名为 API 的目录中并且不要附加另一个API 到路由模式,例如。