当子文件夹中的Codeigniter app时,无法访问rest api方法

时间:2017-12-09 15:07:23

标签: php apache .htaccess codeigniter vhosts

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

这是我的文件夹结构

enter image description here

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

enter image description here

这是我的.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服务器上的vhost(localhost开发) NameVirtualHost *: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页面(没有初始控制器初始化)

enter image description here

访问日志:

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页面

enter image description here

访问日志

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

1 个答案:

答案 0 :(得分:0)

我不知道我究竟做了什么,但我的应用程序如何开始工作......

这是我的.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]

这是我的vhost conf(我认为这解决了问题)

<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>