子目录中的Slim api出错404

时间:2014-07-21 15:46:43

标签: php .htaccess slim

这是我的文件夹结构

channel
   - includes
      - db_connect.php
      - functions.php
   - libs
      - vendor
      - composer.json
   - v1
      - .htacces
      - index.php

这是我的.htaccess文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

我对这一切都不熟悉,我真的不了解.htaccess文件或它的作用。当我尝试使用网址连接时

http://localhost/channel/v1/index.php/test

我收到404错误。这是用于获取测试参数的代码块

$app->post('/test',function(){
    echo 'test is working';
})

我如何让它正常工作?我在ubuuntu做了这一切

1 个答案:

答案 0 :(得分:0)

使用slim(或大多数其他REST服务),您不能使用WEBROOT/index.php/yourdefinedEndpointWEBROOT/yourdefinedEndpoint来调用您的方法。对于$app->post('/test',function(){});,它应该是http:// localhost/channel/v1/test(另请注意:如果您定义了一个帖子路线但是将该页面称为GET(没有表单),则会出现错误。要学习,请尝试{ {1}}

.htaccess文件基本上重写了所有"子文件夹"参数,Slim Framework将为您解析并调用匹配函数。

编辑:请注意,您需要启用.htaccess(请参阅ubuntu Wiki)和mod_rewrite(请参阅this answer)。

此外,我建议使用Slim文档提供的.htaccess