【问题标题】:Can't access rest api method when Codeigniter app in sub folder子文件夹中的 Codeigniter 应用程序时无法访问 rest api 方法
【发布时间】:2018-05-23 14:50:20
【问题描述】:

当我将 Codeigniter 应用程序放入子文件夹时,我无法访问我的 REST api 方法。我已将 Codeigniter 应用程序放在 rest 文件夹中。然后我尝试拨打http://myapp.dev/rest/api/Workingday/workingDay,我得到404 not found,如果我尝试访问http://myapp.dev/rest/index.php,也会出现同样的问题

这是我的文件夹结构

这是我的扩展 rest api 文件夹,所以你可以看到我在控制器中有 api 文件夹,并且会有公共和休息 API。

这是我的 .htaccess

RewriteEngine on
#RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /rest/index.php/$1 [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

这是我在 Apache 服务器上的虚拟主机(本地主机开发) 名称VirtualHost *:80

<VirtualHost *:80>
    ServerName myapp.dev
    ServerAlias *.myapp.dev
    ServerAdmin info@myapp.dev
    DocumentRoot "/Users/user/workspace/myapp" LogLevel debug
    ErrorLog "/Users/user/workspace/myapp/rest/application/logs/myapp.dev-error_log"
    CustomLog "/Users/user/workspace/myapp/rest/application/logs/myapp.dev-access_log" common
</VirtualHost>

这是我的WorkingDay api,以防您在这里看到任何问题,也许......文件的位置是/Users//workspace/myapp/rest/application/controllers/api/Workingday.php

class Workingday extends REST_Controller {

    public function workingDays_get($id=NULL){
        if($id != NULL){
            $condition['id_working_day'] = $id;
            $workingDays = $this->Working_days->get($condition);
        }else{
            $workingDays = $this->Working_days->get();
        }
        if($workingDays){
            $this->response($workingDays, REST_Controller::HTTP_OK);
        }else{
            $this->response([
                'status' => FALSE,
                'message' => 'No time of day data were found'
            ], REST_Controller::HTTP_NOT_FOUND);
        }
    }
}

我认为问题出在 .htaccess 文件中,但我不知道如何正确配置它。如何正确配置我需要的一切,以便进一步开发我的应用程序?

****更新**

我认为访问 http://myapp.dev/rest/index.php 是有效的,因为它显示 Codeigniters 404 页面(只是没有初始化任何初始控制器)

访问日志:

127.0.0.1 - - [09/Dec/2017:17:58:00 +0100] "GET /rest/ HTTP/1.1" 404 1130
127.0.0.1 - - [09/Dec/2017:18:00:54 +0100] "GET /rest/index.php HTTP/1.1" 404 1130

访问http://myapp.dev/rest/api/Workingday/workingDay时显示浏览器404页面

访问日志

127.0.0.1 - - [09/Dec/2017:17:53:18 +0100] "GET /rest/api/Workingday/workingDay HTTP/1.1" 404 228

【问题讨论】:

  • 可能是 RewriteRule,它将所有内容放在 url 行,并将其放在 index.php 之后。因此,如果您尝试转到http://myapp.dev/rest/api/Workingday/workingDay,它会重写为http://myapp.dev/rest/index.php/rest/api/Workingday/workingDay ...不确定是否有效。 (虽然我对 codeigniter 很陌生)
  • 不,它也不能与 url 中的 index.php 一起使用...我在配置 $config['index_page']='' 中有空白值
  • 好吧,通过重写,如果您尝试访问 http://myapp.dev/rest/index.php... 应该将其更改为 http://myapp.dev/rest/index.php/rest/index.php :( 哦...但是,是的,!-f 的条件应该防止这种情况。好的。
  • 那里有很多框架。结帐http://myapp.dev/时指向哪个文件和位置?
  • 当提到自己(“我”)时,请始终使用大写字母。显然,在社交媒体和聊天室中使用小写字母很常见,但在这里我们热衷于可读性(希望您的 141 个其他问题没有以这种方式编写 - 这对志愿编辑来说是很多工作)。

标签: php apache .htaccess codeigniter vhosts


【解决方案1】:

我不知道我到底做了什么,但我的应用程序是如何突然开始工作的......

这是我的 .htaccess 文件

RewriteEngine on
#RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /rest/index.php/$1 [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

这是我的虚拟主机配置(我认为这解决了问题)

<VirtualHost *:80>
    ServerName myApp.dev
    ServerAlias *.myApp.dev
    ServerAdmin info@myApp.dev
    DocumentRoot "/Users/<user>/workspace/myApp"
    LogLevel debug
    ErrorLog "/Users/<user>/workspace/myApp/rest/application/logs/myApp.dev-error_log"
    CustomLog "/Users/<user>/workspace/myApp/rest/application/logs/myApp.dev-access_log" common
    <Directory   "/Users/<user>/workspace/myApp">
        Options Indexes FollowSymLinks
        AllowOverride All
        Allow from all
    </Directory> 
</VirtualHost>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-29
    • 2013-08-21
    • 2018-07-08
    • 2014-09-26
    相关资源
    最近更新 更多