Reputation: 183
Here i have used yii2 configuration with rules
<?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-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'rules' => require(__DIR__ . '/routes.php'),
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
And given below routes.php
<?php
use yii\web\UrlRule;
return array(
// REST routes for CRUD operations
'POST <controller:\w+>s' => '<controller>/create', // 'mode' => UrlRule::PARSING_ONLY will be implicit here
'api/<controller:\w+>s' => '<controller>/index',
'PUT api/<controller:\w+>/<id:\d+>' => '<controller>/update',// 'mode' => UrlRule::PARSING_ONLY will be implicit here
'DELETE api/<controller:\w+>/<id:\d+>' => '<controller>/delete', // 'mode' => UrlRule::PARSING_ONLY will be implicit here
'api/<controller:\w+>/<id:\d+>' => '<controller>/view',
// normal routes for CRUD operations
'<controller:\w+>s/create' => '<controller>/create',
'<controller:\w+>/<id:\d+>/<action:update|delete>' => '<controller>/<action>',
);
When i access local2host.com/advanced/backend/web/index.php/country/create - It's working fine
but when i access through local2host.com/advanced/backend/web/index.php/api/country/create
It's throwing 404 - not found error
Unable to resolve the request "api/country/create".
As per my requirement : when i access this link local2host.com/advanced/backend/web/index.php/api/country/create it should access "country" as controller and "create" as action
Upvotes: 0
Views: 6462
Reputation: 14459
It seems you have modules. First you need to add your api
module into your config file:
'modules' => [
'api'
],
Second, you need to add module into your rules like below:
'<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',
'<module:\w+><controller:\w+>/<action:update|delete>/<id:\d+>' => '<module>/<controller>/<action>',
This is remarkable that, you may need to take care of other rules with module and have your own customized rules.
Upvotes: 2