简体   繁体   中英

404 Not Found for API routes in Yii

I'm trying to bring to life locally a Yii project created by another dev, I'm running Xampp 5.6.28 over Mac.

Currently the project is serving the backend and frontend urls but the api routes throw 404 Not Found errors.

Apache conf is:

<Directory "/Applications/XAMPP/xamppfiles/htdocs">
    # use mod_rewrite for pretty URL support
    RewriteEngine on
    # If a directory or a file exists, use the request directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Otherwise forward the request to index.php
    RewriteRule . index.php

    # XAMPP
    Options Indexes FollowSymLinks ExecCGI Includes

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    #AllowOverride None
    # since XAMPP 1.4:
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

The app follows the "Yii 2 Advanced Project Template" structure, the api is in: app/api/ and the controllers that should be responding are at: app/api/modules/v1/controllers/AppController.php

There's an api/config/main.php with this:

<?php
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [
    'id' => 'app-api',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'api\controllers',
    'defaultRoute'=>'default',
    'bootstrap' => ['log'],
    'modules' => [
        'v1' => [
            'class' => 'api\modules\v1\Module',
        ],
    ],
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => false,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'request' => [
                'enableCsrfValidation'=>false,
                'parsers' => [
                        'application/json' => 'yii\web\JsonParser',
                ]
        ],
        'errorHandler' => [
                'errorAction' => 'default/error',
        ],
        'urlManager'=>[
                'enablePrettyUrl'=>true,
                'showScriptName'=>false,
        ],
    ],
    'params' => $params,
];

And a common/config/main.php with:

<?php
return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'name' => 'FULOOP',
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'authManager' => [
                'class' => 'yii\rbac\DbManager',
        ],
        'formatter' => [
                'class' => 'yii\i18n\Formatter',
                'dateFormat' => 'php:m/d/Y',
                'datetimeFormat' => 'php:m/d/Y H:i:s',
                'timeFormat' => 'php:H:i:s',
        ],              
                'frontendUrlManager'=>[
           'class' => 'yii\web\urlManager',
           'enablePrettyUrl'=>true,
           'showScriptName'=>false,
           'baseUrl'=>'/fuloop/frontend/web',
       ],
    ],
];

I'm not familiar with the Yii Framework so I'm not sure where exactly should I be looking for this, thanks in advance for any help.

Do you really locate your project on this path?

<Directory "/Applications/XAMPP/xamppfiles/htdocs">

Because this is my local XAMP config.

And you can see what parameter Directory use path to web dir into project dir

<VirtualHost *:80> 
 ServerName app-rest.dev
 DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/app-proj/web/"  
<Directory "/Applications/XAMPP/xamppfiles/htdocs/app-proj/web/" > 
  Options Indexes FollowSymLinks Includes execCGI 
  AllowOverride All 
  Allow From All 
  Order Allow,Deny 
  RewriteEngine on
 </Directory> 
</VirtualHost>

Also, you can find helpful information in this guide

Eg in my config/web.php I use this rule for api controllers

 'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            ['class' => 'yii\rest\UrlRule', 
             'pluralize'=>false,
             'controller' => 'v1/app',  
                'patterns'   => [
                    'OPTIONS <action>' => 'options', 
                ],
             ],
         ],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM