(05-05-1395، 01:36 ب.ظ)ADMIN نوشته: این لینک رو مطالعه کنید (همراه با بخشهای فرعی داخلش) :
http://www.yiiframework.com/doc-2.0/guid...start.html
این ها رو مطالعه کردم ولی جواب نداد. بعضی ها با header("Access-Control-Allow-Origin: *"); جواب گرفتند ولی من تست کردم جواب نداد.
کد کنترلر :
<?php
namespace appcontrollers;
use appmodelsSettings;
use yii;
use yiirestActiveController;
use yiidataActiveDataProvider;
use appmodelsCategory;
use yiifiltersauthHttpBasicAuth;
use appdataAppLocale;
class CategoryController extends ActiveController
{
public $modelClass = 'appmodelsCategory';
private $language;
public function init()
{
parent::init();
// set language
$this->language=substr(Yii::$app->request->getQueryParam('lang',Settings::defaultLanguage()),0,2);
}
public function behaviors()
{
$behaviors = parent::behaviors();
// remove authentication filter
$auth = $behaviors['authenticator'];
unset($behaviors['authenticator']);
// add CORS filter
$behaviors['corsFilter'] = [
'class' => yiifiltersCors::className(),
];
// re-add authentication filter
$behaviors['authenticator'] = $auth;
// avoid authentication on CORS-pre-flight requests (HTTP OPTIONS method)
$behaviors['authenticator']['except'] = ['options'];
return $behaviors;
}
public function actions()
{
$actions = parent::actions();
// remove the "create", "update", "options" and "delete" actions
unset($actions['delete'], $actions['create'],$actions['update'],$actions['options']);
// customize the data provider preparation with the "prepareDataProvider()" method
$actions['index']['prepareDataProvider'] = [$this, 'getCategories'];
// set view action $findModel
$actions['view']['findModel']=function ($id, $action) {
$data=Category::find()->select([
'category.category_id','category.picture',
'trn.name',
])->innerJoinWith([
'translates trn' => function ($query) {
$query->andWhere(['=', 'trn.language', $this->language]);
},
])->where(['category.active'=>1,'category.category_id'=>$id])->one();
return $data ? $data : [];
};
return $actions;
}
/**
* @param ActionObject $action
* @return mixed
*/
public function getCategories($action)
{
// prepare and return a data provider for the "index" action
$req=Yii::$app->request;
$parent=$req->getQueryParam('parent',null);
// set language
$this->language=AppLocale::getRestLanguage();
// query of select categories
$query=Category::find()->select([
'category.category_id','category.picture',
'trn.name',
])->innerJoinWith([
'translates trn' => function ($query) {
$query->andWhere(['like', 'trn.language', $this->language]);
},
])->where(['category.active'=>1]);
if(!is_null($parent)) {
// set parent condition
$parent = (int)$parent;
if($parent > 0)
$query->andWhere(['category.parent'=>$parent]);
} else {
// set root condition
$query->andWhere(['category.parent'=>null]);
}
// You can prepare $query here
return new ActiveDataProvider([
'query' => $query,
]);
}
}
?>