PHP Slim - 如何为不同的路由设置多个应用程序对象?
PHP Slim - how to have multiple app objects for different routes?
一个网站的不同 sections/routes 是否可以有多个 Slim 应用程序对象。
例如:
- /api -> 有自己的 Slim 对象,有自己的路由、配置等
- /admin-panel -> 有自己的 Slim 对象(可能有一些中间件)
我已经尝试修改 Apache 的 .htaccess 使用:
RewriteRule ^api front_controller_api.inc.php [QSA,L]
RewriteRule ^admin-panel front_controller_admin.inc.php [QSA,L]
...但这似乎打破了 Slim 的路由原则,因为 Slim 认为 /api 和 /admin-panel 是请求 URI 的一部分。对于页面的每个部分,使用具有不同配置、中间件等的不同应用程序对象会容易得多。
有什么想法吗?
我不知道这样做是否正确,但您可以试试这样的文件夹结构:
public/
|-> api/
|-> index.php
|-> .htaccess
|-> admin-panel/
|-> index.php
|-> .htaccess
更新:
我 "investigated" 想出了另一个解决方案:
public/
|-> .htaccess
|-> admin-panel.php
|-> api.php
.htaccess
:
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_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin-panel/ admin-panel.php [QSA,L]
RewriteRule ^api/ api.php [QSA,L]
更新 2:
使用此解决方案,您必须在路由定义中将所有内容分组到 '/admin-panel'
或 '/api'
。
您可以使用 groups 轻松完成此操作:
$app->group('/api', function () use ($app){
//Your api scope
$app->myCustom = "my custom";
$app->get('/', function () use ($app) {
echo $app->myCustom;
});
});
//Your amazing middleware.
function adminPanelMiddleware() {
echo "This is my custom middleware!<br/>";
}
$app->group('/admin-panel', 'adminPanelMiddleware', function () use ($app){
//Your admin-panel scope
$app->anotherCustom = "another custom";
$app->get('/', function () use ($app) {
echo $app->anotherCustom;
});
});
一个网站的不同 sections/routes 是否可以有多个 Slim 应用程序对象。
例如:
- /api -> 有自己的 Slim 对象,有自己的路由、配置等
- /admin-panel -> 有自己的 Slim 对象(可能有一些中间件)
我已经尝试修改 Apache 的 .htaccess 使用:
RewriteRule ^api front_controller_api.inc.php [QSA,L]
RewriteRule ^admin-panel front_controller_admin.inc.php [QSA,L]
...但这似乎打破了 Slim 的路由原则,因为 Slim 认为 /api 和 /admin-panel 是请求 URI 的一部分。对于页面的每个部分,使用具有不同配置、中间件等的不同应用程序对象会容易得多。
有什么想法吗?
我不知道这样做是否正确,但您可以试试这样的文件夹结构:
public/
|-> api/
|-> index.php
|-> .htaccess
|-> admin-panel/
|-> index.php
|-> .htaccess
更新:
我 "investigated" 想出了另一个解决方案:
public/
|-> .htaccess
|-> admin-panel.php
|-> api.php
.htaccess
:
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_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin-panel/ admin-panel.php [QSA,L]
RewriteRule ^api/ api.php [QSA,L]
更新 2:
使用此解决方案,您必须在路由定义中将所有内容分组到 '/admin-panel'
或 '/api'
。
您可以使用 groups 轻松完成此操作:
$app->group('/api', function () use ($app){
//Your api scope
$app->myCustom = "my custom";
$app->get('/', function () use ($app) {
echo $app->myCustom;
});
});
//Your amazing middleware.
function adminPanelMiddleware() {
echo "This is my custom middleware!<br/>";
}
$app->group('/admin-panel', 'adminPanelMiddleware', function () use ($app){
//Your admin-panel scope
$app->anotherCustom = "another custom";
$app->get('/', function () use ($app) {
echo $app->anotherCustom;
});
});