htaccess 子域到使用 Slim Framework 不可见的域文件夹

htacess subdomain to domain folder invisible with Slim Framework

我来这里是想请教您针对这种情况的最佳实践。我在 Slim / rewrite / htaccess 上提出了一些其他问题,但没有成功。到这里没遇到什么问题,只是不知道是不是个好习惯

因此,对于 Slim Framework 3,我有一个主域 www.domain.com 和一个子域 api.domain.com.

当我输入地址栏api.domain.com/messages时,它调用www.domain.com/api/messages 透明,无重定向。

为了实现这个技巧,我把它放在我的 index.php 文件中:

if ($_SERVER['HTTP_HOST'] == 'api.domain.com') {
  $_SERVER['REQUEST_URI'] = '/api' . $_SERVER['REQUEST_URI'];
}

效果很好,我不想花时间重写规则...但如果有人有建议,我将不胜感激!

感谢阅读!

一种方法是在您的根目录中创建一个 api 目录,其中包含一个 index.php 文件来处理您的 api 请求。所以在你的 public/index.php 文件中你可以添加:

// public/index.php
chdir(dirname(__DIR__));

require_once 'vendor/autoload.php';

// api domain so include the api routes
if ($_SERVER['HTTP_HOST'] == "api.domain.com") {
    require 'api/index.php';
    exit;
}

// --------------------------------------------
// non api domain

$app = new Slim\App;

$app->get('/',function($request,$response) {
    return $response->write("I'm not the API site!");
});

$app->run();

然后在 api/index.php 文件中单独处理您的 api 路由:

// api/index.php
$app = new Slim\App;

$app->get('/',function($request,$response) {
    return $response->withJson('base api');
});

$app->group('/game',function() {

    $this->get('',function($request,$response) {
        return $response->withJson('Select a game');
    });

    $this->get('/space-invaders',function($request,$response) {
        return $response->withJson('Space Invaders API');
    });

    $this->get('/pacman',function($request,$response) {
        return $response->withJson('Pac Man API');
    });
});

$app->run();

是的,就是这样!就我而言,我为每个子域创建了一个文件夹,一个用于我的网站,另一个用于 public :

  • public
  • 来源
    • api
    • 游戏
    • 应用
    • 管理员
    • www
  • 供应商

所有子域都指向我的 index.phppublic 文件夹中。然后 index.phphttp_host 切换询问文件夹中的好应用程序文件(如 src/api/app.php ).

在每个子文件夹(api、admin、...)中,与应用程序文件、数据库架构、是否有视图、资源、...

像这样,所有部分都有独立的文件系统,但它们共享相同的数据库和供应商。我防止我需要这种结构来满足特定需求。我不想为每个部分安装一个 Slim...

感谢您的帮助!