【问题标题】:Yii2: REST API - 404 errorYii2:REST API - 404 错误
【发布时间】:2016-07-17 04:15:24
【问题描述】:

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' =&gt; ['tw' =&gt; 'tweet'] whit 根据 url。

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

补充说:嗯... url 应该是 http://localhost/index.php/tweets 但对我来说不是很明显。

【问题讨论】:

  • 请显示您的 TweetController 代码的所有代码
  • 您使用的是基本模板还是高级模板?你的项目是哪个目录..?
  • 基本模板。 Em 你是什么意思“哪个”? yii2 - 名称。
  • 您的项目保存在一个目录中...您的项目目录的路径...例如:localhost/mybasicproject
  • 我已经发布了第一个建议的答案

标签: php rest yii2


【解决方案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/

【讨论】:

  • 是的,我阅读了该指南,但我尝试找出原因……我和快速入门指南一样。但在我的情况下是 url http://localhost/index.php/tweets 我不明白为什么会有区别
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    将以下行添加到您的控制器:

    protected function verbs() {
       $verbs = parent::verbs();
        $verbs =  [
            'index' => ['GET', 'POST', 'HEAD'],
            'view' => ['GET', 'HEAD'],
            'create' => ['POST'],
            'update' => ['PUT', 'PATCH']
        ];
       return $verbs;
    }
    

    【讨论】:

    • 虽然这可能会解决问题,但请考虑解释为什么会这样。
    【解决方案4】:

    我刚遇到这个问题,花了一些时间跟踪代码,发现您必须为您的项目定义一个别名。没有它,它就无法加载你的控制器。

    我正在使用 Yii2 高级模板设置,我有这个结构:

    /common
    /mainApp
    /api
    

    /common/config/bootstrap.php,我不得不添加:

    Yii::setAlias('@api', dirname(dirname(__DIR__)) . '/api');
    

    这允许控制器在 Yii 2 框架中查找和加载/创建过程正常工作。没有它,第 637 行 yii2\base\Module.php 的 class_exists 签入将失败并导致请求导致 404。

    希望这对某人有所帮助!

    ~干杯:)

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题,我设置了'enableStrictParsing' =&gt; false,它对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-12
        • 2018-10-05
        • 2020-04-12
        • 1970-01-01
        • 2015-07-31
        • 2019-07-14
        相关资源
        最近更新 更多