【问题标题】:PHP Silex routes base urlPHP Silex 路由基本 url
【发布时间】:2023-04-01 12:35:01
【问题描述】:

我的项目只是本地服务器上子文件夹中的一个测试 API 项目。所以我的网址是http://localhost/test-project-x/api/。里面有一个index.php。

可以通过上面的 URL 获得根路由。但是一旦我转到http://localhost/test-project-x/api/test,我就会得到一个 404。要让它工作,我需要将 PHP 路由从 $app->get('/test' ... 更改为 $app->get('/test-project-x/api/test' ...

我希望它与 /test 一起使用。但是对于我的生活,我似乎无法弄清楚/记得如何......

test-project-x/api/index.php:

<?php    
require_once __DIR__.'/../../vendor/autoload.php';

$app = new Silex\Application();
$app['debug'] = true;

$app->get('/test', function() use($app) {
    return 'This would be the test route.';
});

$app->get('/', function() use($app) {
    return "This would be the root.";
});

$app->run();

test-project-x/api/.htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    #RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>

【问题讨论】:

    标签: routes silex base-url


    【解决方案1】:

    只需将 RewriteBase 与正确的文件夹一起使用:

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /test-project-x/api/
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ index.php [QSA,L] 
    </IfModule>
    

    你有,你的路线现在在 /test-project-x/api 文件夹中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多