Reputation: 539
DataBase has table with saved Tweets.
There is controller:
<?php
namespace app\controllers;
use yii\rest\ActiveController;
class TweetController extends ActiveController
{
public $modelClass = 'app\models\Tweet';
}
Corresponding model app\model\Tweet
created by gii
.
In app\web\config
added:
..............
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule',
'controller' => 'tweet'],
],
],
'request' => [
'parsers' =>['application/json' => 'yii\web\JsonParser', ],
],
...............
In app\web
added .htaccess
according http://www.yiiframework.com/doc-2.0/guide-tutorial-shared-hosting.html
In apache
DocumentRoot as app\web
According yii2 docs: curl -i -H "Accept:application/json" "http://localhost/tweets"
must return paged model data. Instead of this:
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>
Also tryed - urlManager 'controller' => ['tw' => 'tweet']
whit according url.
why there is 404? Guided by http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html
added: well... url should be http://localhost/index.php/tweets
but it`s not obviously for me.
Upvotes: 1
Views: 7248
Reputation: 31
I had the same problem, I set 'enableStrictParsing' => false
and it worked for me.
Upvotes: 1
Reputation: 166
I just encountered this issue and spent some time tracing through the code to find that you must have an alias defined for your project. Without that, it cannot load your controllers.
I am working in a Yii2 advanced template setup where I have this structure:
/common
/mainApp
/api
In /common/config/bootstrap.php
, I had to add:
Yii::setAlias('@api', dirname(dirname(__DIR__)) . '/api');
This allows the controller find and load/create process in the Yii 2 framework to work correctly. Without it, the class_exists check in yii2\base\Module.php
on line 637 will fail and cause the request to result in a 404.
Hope this helps someone!
~Cheers :)
Upvotes: 0
Reputation: 115
add followings lines to your controller:
protected function verbs() {
$verbs = parent::verbs();
$verbs = [
'index' => ['GET', 'POST', 'HEAD'],
'view' => ['GET', 'HEAD'],
'create' => ['POST'],
'update' => ['PUT', 'PATCH']
];
return $verbs;
}
Upvotes: 0
Reputation: 11
I also got the same problem.This is my solution,which worked well for me(Apache 2.2). **Editing the apache2.conf file and changing the AllowOverride from None to All. Then restart Apache Service **
Upvotes: 1
Reputation: 133400
The path should be
http:://localhost/yii2/tweets
or
http:://localhost/yii2/index.php/tweets
(depending by the correct configuration of urlManager)
try also
http:://localhost/yii2/tweets/index
or
http:://localhost/yii2/index.php/tweets/index
could be you can find useful this tutorial http://budiirawan.com/setup-restful-api-yii2/
Upvotes: 1