Yii2 freymvorkida VerbFilter klasi

LxaOnsbAFgTo8KWkMmbi_30.jpg

VerbFilter - Yii2dagi tayyor filter. Uning vazifasi berilgan acition uchun belgilangan so'rov turi(m: POST, GET, PUT) bo'yicha cheklov o'rnatadi.

Filterdan foydalanish uchun kontroller behaviorida filter klas nomini ko'rsatish kerak.

Masalan, agar index actioniga faqat GET so'rovi bilan murojaat qilishini belgilashimiz, delete uchun esa POSTni berishimiz kerak bo'lsin:

use yii\filters\VerbFilter;
 
class PostController extends Controller
{
            public function behaviors()
            {
                        return [
                                   'verbs' => [
                                               'class' => VerbFilter::className(),
                                               'actions' => [
                                                           'index' => ['get'],
                                                           'delete' => ['post']
                                               ]
                                   ]
                        ];
            }
}

 

Shu holatda yuqoridagi actionlarga filterda berilgan so'rov turidan boshqachasi bilan murojaat qilinsa Method Not Allowed (# 405) xatoligi chiqadi.

Manbaa: Klisl.com