user199588
user199588

Reputation: 539

Yii2: REST API - 404 error

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

Answers (5)

Sagar Mishra
Sagar Mishra

Reputation: 31

I had the same problem, I set 'enableStrictParsing' => false and it worked for me.

Upvotes: 1

darksnake747
darksnake747

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

Behrooz Moghaddam
Behrooz Moghaddam

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

Spground
Spground

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

ScaisEdge
ScaisEdge

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

Related Questions