guillefix
guillefix

Reputation: 992

Yii api routes return 404

My project has an api folder in the same level as frontend and backend folders.

This is my api/config/main.php file:

<?php
if (isset($_SERVER['APPLICATION_ENV'])) {
    $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')
    );
} else {
    $params = array_merge(
        require(__DIR__ . '/../../common/config/params.php'),
        require(__DIR__ . '/params.php')
    );
}

return [
    'id' => 'app-api',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
        'response' => [
            'format' => yii\web\Response::FORMAT_JSON,
            'charset' => 'UTF-8',
        ],
        'request' => [
            'enableCookieValidation' => true,
            'enableCsrfValidation' => true,
            'cookieValidationKey' => 'uwu',
        ],
        'user' => [
            'identityClass' => 'common\modules\user\models\User',
            'enableAutoLogin' => false,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => false,
            'showScriptName' => false,
            'rules' => [
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => 'v1/user',
                    'pluralize' => false
                ],
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => 'v1/code',
                    'pluralize' => false
                ]
            ],
        ]
    ],
    'modules' => [
        'v1' => [
            'basePath' => '@api/modules/v1',
            'class' => 'api\modules\v1\Module',
            'controllerNamespace' => 'api\modules\v1\controllers'
        ]
    ],
    'params' => $params,
];

I've tried accessing these different urls but all I get is a 404 Not Found response from apache2:

And with this URL I get a 404 Not found JSON Response generated by Yii, which says that the Route is invalid and unable to resolve the request:

Upvotes: 0

Views: 375

Answers (1)

Tibor Nagy
Tibor Nagy

Reputation: 1236

In Yii advanced template you have to use different hostname if you want something on the same level as backend and frontend.

You have to define the virtual host in the apache configuration in the directory /etc/apache2/sites-available directory linked to the sites-enabled directory:

<VirtualHost *:80>
        DocumentRoot /YOUR_PROJECT_DIR/api/web
        ServerName YOUR.SERVER
        <Directory /YOUR_PROJECT_DIR/api/web>
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>

Furthermore analyze the e.g. the frontend/web directory, specially the file .htaccess and index.php to use your api tier.

Upvotes: 2

Related Questions