0
votes

I have problems with Yii2 in setting up an application for API inside the advanced template. I cannot get any input but a 404 calling the module with v1 of my API

I'm trying to call the api with the address: http://localhost/HeyConference/api/v1/country

I have the following tutorials / templates:

as well as request for support for the same problem, with no success.

Yii version: 2.0.10 PHP version: 5.6.23 and PHP version: 5.5 (XAMP)

I suspect the problem is in my config

My file structure is:

  • _protected
    • api
      • config
        • bootstrap.php
        • main.php
        • main + local.php
        • params.php
        • params + local.php
      • controllers
        • SiteController.php
      • modules
        • v1
          • controllers
            • Contry.php
          • models
            • Country.php
          • Module.php
      • runtime
        • cache
        • debug
        • log
  • ....
  • api
    • assets
    • .htaccess
    • index.php
    • index
    • test.php
    • robots.txt

here are specific files:

index.php

<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../_protected/vendor/autoload.php');
require(__DIR__ . '/../_protected/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../_protected/common/config/bootstrap.php');
require(__DIR__ . '/../_protected/api/config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../_protected/common/config/main.php'),
    require(__DIR__ . '/../_protected/common/config/main-local.php'),
    require(__DIR__ . '/../_protected/api/config/main.php'),
    require(__DIR__ . '/../_protected/api/config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();

_protected/api/config/main.php

<?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',
    //'controllerNamespace' => 'api\modules\v1\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        'v1' => [
            'basePath' => '@api/modules/v1',
            'class' => 'api\modules\v1\Module',   // here is our v1 modules
            'controllerNamespace' => 'api\modules\v1\controllers',
        ]
    ],
    'components' => [
        'user' => [
            'identityClass' => 'common\models\UserIdentity',
            'enableAutoLogin' => false,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => false, //true,
            'showScriptName' => false,
            'rules' => [
                [
                    //'pluralize' => false,

                    'class' => 'yii\rest\UrlRule',

                    'controller' => ['v1/country', 'v1/conference'],
                    'tokens' => [
                        '{id}' => '<id:\\w+>'
                    ]
                ]
            ],
        ]
    ],
    'params' => $params,
];

_protected/api/modules/v1/Module.php

<?php
namespace api\modules\v1;
class Module extends \yii\base\Module
{
    public $controllerNamespace = 'api\modules\v1\controllers';
    public function init()
    {
        parent::init();
    }
}

_protected/api/modules/v1/controllers/Country.php

<?php
namespace api\modules\v1\controllers;
use yii\rest\ActiveController;
class ConferenceController extends ActiveController
{
    public $modelClass = 'api\modules\v1\models\Conference';
}

_protected/api/modules/v1/controllers/Country.php

<?php
namespace api\modules\v1\controllers;
use yii\rest\ActiveController;
/**
 * Country Controller API
 *
 * @author alex
 */
class CountryController extends ActiveController
{
    public $modelClass = 'api\modules\v1\models\Country';
}

.htaccess file:

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
1
how do you call the action that return 404 ? - scaisEdge
maybe you forget .htaccess file? - Vitaly

1 Answers

0
votes

I see that your controllers don't follow the Yii2 convention. They should be called XXXController.

So, instead of _protected/api/modules/v1/controllers/Country.php your file should be named _protected/api/modules/v1/controllers/CountryController.php