Bilişim dünyasına kaliteli, özgün ve Türkçe içerikler kazandırmayı hedefleyen bir platform..

friends friends friends

Yii2 Framework Throw Exception

throw ifadesi: Eğer bir fonksiyonun exception oluşturma olasılığı varsa ve try/catch bloğu kullanılmak istenmiyorsa özel hata oluşturmak için kullanılır.

finally ifadesi: try catch bloklarından sonra ne olursa olsun kodu çalıştırmak için kullanılır.

try ifadesi: Hata oluşturabilecek kod blokları yerleştirilir.

catch ifadesi: hata oluştuğunda hatayı yakalamak için kullanılır.

if ($item == null) {
	throw new \yii\web\NotFoundHttpException('The requested page does not exist.');
}
if ($item === null) { // item does not exist
    throw new \yii\web\HttpException(404, 'The requested Item could not be found.');
}

Error hata sayfasına yönlendirmek için frontend/web/index.php dosyasında

//defined('YII_DEBUG') or define('YII_DEBUG', true);

yukarıda ki gibi kodları yorum satırına almanız gerekecektir. Ayrıca bir sorgunun sonucu boş geldiği zaman yine aşağıda ki gibi, hata sayfasına yönlendirebilirsiniz. Buna hata sayfasını çağırmakta denir.

$customer = Customer::find()->where(['age' => 30, 'status' => 1])->one();
if ($customer === null){
 throw new \yii\web\NotFoundHttpException("Your Error Message.");
}
Yii2 Framework Throw Exception NotFoundHttpException HttpException 404 not found throw ifadesi try-catch finally
0 Beğeni
Yii2 Framework
Önceki Yazı

Pentest(Sızma Testi)

25 Ekim 2020 tarihinde yayınlandı.
Sonraki Yazı

En passant(Geçerken Alma)

25 Ekim 2020 tarihinde yayınlandı.
arrow