关于 yii2 框架和其他 api
about yii2 framework and rest api
你好,我第一次尝试休息 api,我使用 postman 显示结果,但是当我使用 POST 时出现错误,错误是 { "message": "Method Not Allowed. This url can only handle the following request methods: GET, HEAD.",}
这是我的控制器
<?php
namespace backend\controllers;
use yii\rest\ActiveController;
class TestController extends ActiveController{
public $modelClass='backend\models\test';
}
这是主要的配置文件夹
<?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' => [
'api' => [
'class' => 'backend\modules\api\Api',
],
],
'components' => [
'request' => [
'csrfParam' => '_csrf-backend',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => FALSE,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'test'],
],
],
],
'params' => $params,
];
您必须将参数 pluralize
设置为 false
:
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'test',
'pluralize' => false,
],
],
或尝试通过URL访问它:127.0.0.1/advanced/backend/web/tests
。
更多关于 pluralize
here。
你好,我第一次尝试休息 api,我使用 postman 显示结果,但是当我使用 POST 时出现错误,错误是 { "message": "Method Not Allowed. This url can only handle the following request methods: GET, HEAD.",}
这是我的控制器
<?php
namespace backend\controllers;
use yii\rest\ActiveController;
class TestController extends ActiveController{
public $modelClass='backend\models\test';
}
这是主要的配置文件夹
<?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' => [
'api' => [
'class' => 'backend\modules\api\Api',
],
],
'components' => [
'request' => [
'csrfParam' => '_csrf-backend',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => FALSE,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'test'],
],
],
],
'params' => $params,
];
您必须将参数 pluralize
设置为 false
:
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'test',
'pluralize' => false,
],
],
或尝试通过URL访问它:127.0.0.1/advanced/backend/web/tests
。
更多关于 pluralize
here。