<?php namespace api\controllers; use yii\rest\ActiveController; class UserController extend extends ActiveController { public $modelClass = 'common\models\User'; public function actions() { $action= parent::actions(); // TODO: Change the autogenerated stub unset($action['index']); unset($action['create']); unset($action['update']); unset($action['delete']); } public function actionIndex() { //你的代码 } }
这样我们就可以重写他的代码了。哈哈
我们再新建一个自己的 action
<?php namespace api\controllers; use yii\rest\ActiveController; class UserController extends ActiveController { public $modelClass = 'common\models\User'; public function actions() { $action= parent::actions(); // TODO: Change the autogenerated stub unset($action['index']); unset($action['create']); unset($action['update']); unset($action['delete']); } public function actionIndex() { //你的代码 } public function actionTest() //假如是get请求 { //业务逻辑 } }
然后试着访问一下 ,报错?找不到?
报错就对了,那是因为我们没有设置其他路由访问
修改 apiconfigmain.php
'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'showScriptName' => false, 'rules' => [ ['class' => 'yii\rest\UrlRule', 'controller' => 'user', //'pluralize' => false, //设置为false 就可以去掉复数形式了 'extraPatterns'=>[ 'GET send-email'=>'test' ], ], ], ]
接下来重新访问就没有问题了,ps:你自己编写的任何 action 都要在 extraPatterns 进行配置