【问题标题】:Error 404 with Slim api in sub directory子目录中的 Slim api 错误 404
【发布时间】:2014-07-22 00:11:13
【问题描述】:

这是我的文件夹结构

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 文件或它的作用。当我尝试使用 url 连接时

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

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

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

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

【问题讨论】:

    标签: php .htaccess slim


    【解决方案1】:

    对于 slim(或大多数其他 REST 服务),您不会使用 WEBROOT/index.php/yourdefinedEndpoint 而是使用 WEBROOT/yourdefinedEndpoint 调用您的方法。对于$app->post('/test',function(){});,应该是http:// localhost/channel/v1/test(另请注意:如果你定义了一个post route,但将页面调用为GET(没有表单),就会出错。学习,试试$app->get('/test',function(){};

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-05-20
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 2013-10-30
      • 2018-06-26
      • 2012-11-22
      • 2016-04-23
      • 1970-01-01
      相关资源
      最近更新 更多