我正在用silex建立一个网站,我确信这是一个n00b问题。我有我的htaccess设置(根据silex建议)重定向到我的主页,如果一个url不存在。
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /home
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
so/mysite/api/v1/users将正确加载并显示JSON并列出所有用户,但是/mysite/api/v1/somerandomfolder将重定向到主页。一切正常。
我遇到的问题是,当我尝试/mysite/api/v1/users/00001(00001是用户id)时,它将我重定向回主页,而不是显示特定用户的JSON。
我的代码在index.php在我的用户文件夹看起来像这样:
$users = array(
'00001'=> array(
'name' => 'John Doe',
'age' => '53',
'description' => '...',
'image' => 'john.jpg',
),
'00002' => array(
'name' => 'Jane Doe',
'age' => '27',
'description' => '...',
'image' => 'jane.jpg',
),
);
$app->get('/', function() use ($users) {
return json_encode($users);
});
$app->get('/{userid}', function (Silex'Application $app, $userid) use ($users) {
if (!isset($users[$userid])) {
$app->abort(404, "User ID {$userid} does not exist.");
}
return json_encode($users[$userid]);
});
$app->run();
我肯定有一些基本的概念我错过了,但我不知道它是什么
路由
$app->get('/mysite/api/v1/users/{userid}', function (Silex'Application $app, $userid) use ($users) {
}
RewriteBase必须是站点根目录的路径,相对于文档根目录
我在我的Silex应用程序中运行了您的代码,它似乎工作得很好。
我认为你可能使用多个前端控制器(多个index.php文件)。这可能是可以实现的,但它违背了通常的MVC单入口点概念。让我知道如果我错了关于多个index.php文件。