2
我想在我的控制器中添加登錄操作,並在給出頁面時找不到錯誤。 我知道有很多這樣的帖子,但沒有人幫助過我。我控制器如何將自定義操作添加到yii2的yii rest activecontroller擴展控制器中?
代碼:
<?php
// controller for rest apis
namespace app\controllers;
use yii\rest\ActiveController;
use yii\rest\Controller;
use yii\web\Response;
//use yii\web\Controller;
use app\models\UserMaster;
//class UserController extends Controller
class UserController extends ActiveController
{
public $modelClass = 'app\models\UserMaster';
public $pluralize = false;
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
return $behaviors;
}
public function actionLogin(){
return UserMaster::find()->all();
}
public function login(){
return UserMaster::find()->all();
}
}
?>
從配置/ web.php代碼:
'urlManager' => [
'enablePrettyUrl' => true,
'class'=>'yii\web\UrlManager',
'showScriptName' => false,
//for specifying rest api model to be used
//and by pluralize=false we can use with singular name rather than plural as default (users)
'enableStrictParsing' => true,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'user',
'pluralize'=>false,
'extraPatterns' => [
'GET /user/login/' => 'login', // 'xxxxx' refers to 'actionXxxxx'
],
]
],
],
爲什麼不使用$ behaviors ['authenticator']進行身份驗證?你知道嗎? – yafater
似乎沒問題,只需在extraPatterns中將'GET/user/login /'更改爲'GET登錄'即可。 (現在它試圖從/ user/user/login中獲取)。 – karpy47