【问题标题】:htacess subdomain to domain folder invisible with Slim Frameworkhtaccess 子域到域文件夹在 Slim 框架中不可见
【发布时间】:2017-03-23 17:59:39
【问题描述】:

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

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

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

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

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

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

感谢您的阅读!

【问题讨论】:

  • 如果您的应用程序已经解释 /api 端点,为什么还要使用子域?
  • 嗨!我想将来将应用程序、游戏结果、arduino 和开放数据插入此 api,并且我在 www.domain.com 上有一个网站。我更喜欢像 api.domain.com/game/space-invaders 这样的地址,而不是 www.domain.com/api/game/space-invaders

标签: php .htaccess subdomain slim


【解决方案1】:

您可以做到这一点的一种方法是在您的根目录中创建一个 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();

【讨论】:

    【解决方案2】:

    是的,就是这样!对于我的情况,我为每个子域创建了一个文件夹,一个用于我的网站,一个用于公共:

    • 公开
      • API
      • 游戏
      • 应用程序
      • 管理员
      • www
    • 供应商

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

    在每个子文件夹(api、admin、...)中,与应用程序文件、数据库架构、视图或非视图、资源、...的结构相同。

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

    感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 2012-05-19
      • 1970-01-01
      • 2012-09-14
      • 2019-07-10
      相关资源
      最近更新 更多