首页 文章

Yii2:REST API - 404错误

提问于
浏览
1

DataBase具有已保存推文的表 .

有控制器:

<?php

namespace app\controllers;

use yii\rest\ActiveController;

class TweetController extends ActiveController
{
    public $modelClass = 'app\models\Tweet';
}

对应型号 app\model\Tweetgii 创建 .

app\web\config 补充说:

..............
    'components' => [
        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule',
                 'controller' => 'tweet'],
            ],
        ],
        'request' => [
                 'parsers' =>['application/json' => 'yii\web\JsonParser', ],
        ],
...............

app\web 添加 .htaccess 根据http://www.yiiframework.com/doc-2.0/guide-tutorial-shared-hosting.html

apache DocumentRoot中为 app\web

根据yii2文档: curl -i -H "Accept:application/json" "http://localhost/tweets" 必须返回分页模型数据 . 而不是这个:

HTTP/1.1 404 Not Found
Date: Tue, 29 Mar 2016 14:04:05 GMT
Server: Apache/2.4.7 (Ubuntu)
Content-Length: 278
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /tweets was not found on this server.</p>
<hr>
<address>Apache/2.4.7 (Ubuntu) Server at localhost Port 80</address>
</body></html>

也尝试了 - urlManager 'controller' => ['tw' => 'tweet'] whit根据网址 .

为什么有404?以http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html为指导

补充说:嗯...网址应该是 http://localhost/index.php/tweets 但对我来说并不明显 .

2 回答

  • 1

    路径应该是

    http:://localhost/yii2/tweets
    

    要么

    http:://localhost/yii2/index.php/tweets
    

    (取决于urlManager的正确配置)

    也尝试

    http:://localhost/yii2/tweets/index
    

    要么

    http:://localhost/yii2/index.php/tweets/index
    

    你可以找到有用的教程http://budiirawan.com/setup-restful-api-yii2/

  • 0

    我也遇到了同样的问题 . 这是我的解决方案,对我来说效果很好(Apache 2.2) . 编辑apache2.conf文件并将AllowOverride从None更改为All . 然后重启Apache服务

相关问题