API/V1 /控制器內我的控制器文件/文件上傳yii2
class ProfileController extends ActiveController
{
public $modelClass = 'app\models\Profile';
public function behaviors()
{
return [
[
'class' => 'yii\filters\ContentNegotiator',
'only' =>
['index', 'view', 'createnew','update','search'],
'formats' =>
['application/json' => Response::FORMAT_JSON,],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'index' => ['get'],
'view' => ['get'],
'createnew' => ['post'],
'update' => ['put'],
'delete' => ['delete'],
'deleteall' => ['post'],
'search' => ['get']
],
]
];
}
public function actionCreatenew() {
$model = new Profile();
$model->load(Yii::$app->request->post());
$model->asset = UploadedFile::getInstance($model, 'asset');
$name = $model->user_id;
if($model->asset) {
$model->asset->saveAs('uploads/'.$name.'.
'.$model->asset->extension);
$model->asset = $model->asset->name.'.'.
$model->asset->extension;
}
if($model->save()) {
echo json_encode(array('status'=>"Success",
'data'=>$model->attributes),JSON_PRETTY_PRINT);
} else {
echo json_encode(array('status'=>"Failure",
'error_code'=>400,
'errors'=>$model->errors),JSON_PRETTY_PRINT);
}
}
}
當我嘗試使用上網本從郵差一樣: POST http://localhost/myapp/api/v1/profiles
我獲取無效參數 - yii \ base \ InvalidParamException
響應內容不能是數組。
什麼問題?幫助將不勝感激!由於
我通過在我的api/config.php文件中添加了這個問題''urlManager'=> ['enablePrettyUrl'=> true,'enableStrictParsing'=> true,'showScriptName'=> false,'rules' => [['class'=>'yii \ rest \ UrlRule','controller'=> ['v1/country','v1/profile'],'extraPatterns'=> ['POST createnew'=>'createnew ',],],],],' – Jackhad