yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.91k forks source link

REST apis giving error 404 after upgrading to yii2.0.40 #18454

Closed abayomiabayomi closed 3 years ago

abayomiabayomi commented 3 years ago

I have just upgraded my Yii 2.0 app to 2.0.40. The app has served me well without issues. However, after update my rest api no longer works. It gives a consistent "Not Found (#404). Page not found" error. Whenever i go back to my backup previous Yii version, the rest api works well there but it just will not work on 2.0.40. This issue occurs both on my local server and web server

What steps will reproduce the problem?

Upgrade from yii2.0.1 to yii2.0.40 and PHP from PHP 7.1 to PHP 7.3 After upgrade, i call the api here http://localhost/gig_books/backend/web/index.php/api/mybook

What is the expected result?

It displays the requested content in json format

What do you get instead?

Not Found (#404) Page not found. The above error occurred while the Web server was processing your request. Please contact us if you think this is a server error. Thank you.

Additional info

gig_books/common/config/main-local.php

<?php
return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=gigs',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',

          'useFileTransport' => true,
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'rules' => [
                // your rules go here
            ],
        ],      
    ],
];

gig_books/backend/controllers/ApiController.php

<?php

namespace backend\controllers;

use backend\models\Book;
use yii;
use yii\web\Controller;

class APIController extends Controller
{
    public $enableCsrfValidation = false;  

// =============================================================  Book  ==========================================================

    public function actionMybook()
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $realbook = Book::find()->where('new_id = 3')->limit(1)->all();
        $data = array();
        if(count($realbook)>0)
        {
            foreach($realbook as $item)
            {
                $path = "http://$_SERVER[HTTP_HOST]" . Yii::$app->urlManager->baseUrl;
                $image = $path . '/images/books/' . $item->image;
                $image = str_replace(" ", "%20", $image);

                // full
                if(strlen($item->full)>0)
                {
                    $fileFull = $path . 'uploads/full/' . $item->full;
                    $fileFull = str_replace("web", "", $fileFull);
                }
                else
                {
                    $fileFull = '';
                }

                $data[] = array(
                    'id'=>$item->id,
                    'new_id'=>$item->new_id,
                    'title'=>$item->title,
                    'author'=>$item->author,
                    'price'=>$item->price,
                    'description'=>$item->description,
                    'image'=>$image,
                    'full'=>$fileFull
                );
            }
            return array(
                'status' => 'SUCCESS',
                'data' => $data,
                'message' => 'OK',
            );
        }else
        {
            return array(
                'status' => 'ERROR',
                'message' => 'Not Found Data',
            );
        }
    }

}
Q A
Yii version 2.0.40
PHP version 7.3
Operating system Windows
samdark commented 3 years ago

How about 2.0.39?

abayomiabayomi commented 3 years ago

How about 2.0.39?

I have downgraded to 2.0.39 and the issue remains. Rest api still does not work.

bizley commented 3 years ago

Your action is mybook - how do you have this configured that you expect api/books to work?

abayomiabayomi commented 3 years ago

Your action is mybook - how do you have this configured that you expect api/books to work?

Yes action is mybook and url is like this http://localhost/gig_books/backend/web/index.php/api/mybook. I made a typo error when posting. Error remains. I have downgraded as far back as 2.0.15 and its same issue but when i go back to the original version, api works okay.

bizley commented 3 years ago

I'm not sure if can find the cause of it without your help. Could you debug the request to find the place where you are getting NotFoundHttpException? Switching on the debug logs with trace level and showing the output would be helpful as well.

abayomiabayomi commented 3 years ago

I'm not sure if can find the cause of it without your help. Could you debug the request to find the place where you are getting NotFoundHttpException? Switching on the debug logs with trace level and showing the output would be helpful as well.

Thank you bizley. These are the logs:

2020-12-30 05:25:18 [::1][4][e3c1ss96o0ant3el1b3kjso7g6][error][yii\web\HttpException:404] yii\base\InvalidRouteException: Unable to resolve the request "api/mybook". in C:\wamp64\www\gig_books\vendor\yiisoft\yii2\base\Module.php:537
Stack trace:
#0 C:\wamp64\www\gig_books\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('api/mybook', Array)
#1 C:\wamp64\www\gig_books\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#2 C:\wamp64\www\gig_books\backend\web\index.php(21): yii\base\Application->run()
#3 {main}

Next yii\web\NotFoundHttpException: Page not found. in C:\wamp64\www\gig_books\vendor\yiisoft\yii2\web\Application.php:115
Stack trace:
#0 C:\wamp64\www\gig_books\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#1 C:\wamp64\www\gig_books\backend\web\index.php(21): yii\base\Application->run()
#2 {main}
2020-12-30 05:25:18 [::1][4][e3c1ss96o0ant3el1b3kjso7g6][info][application] $_GET = []
bizley commented 3 years ago

Setting log level to trace would be better. I'll try to dry-run the 2.0.1 version and see what are the differences in handling this.

bizley commented 3 years ago

Ok, I think I found it. It's all because of this line in Module::createControllerByID().

Your controller is in the file named ApiController.php but your controller is named APIController (notice uppercase API).

The check mentioned above is there to prevent such mistakes and it was introduced in 2.0.4.

abayomiabayomi commented 3 years ago

Ok, I think I found it. It's all because of this line in Module::createControllerByID().

Your controller is in the file named ApiController.php but your controller is named APIController (notice uppercase API).

The check mentioned above is there to prevent such mistakes and it was introduced in 2.0.4.

Whao!!! Thats it. It worked. Thank you @bizley