【发布时间】: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\Tweet由gii创建。
在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 根据 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
-
我已经发布了第一个建议的答案