Hello
I am a bit lost on this one, I am trying to upload a file form the form but keep getting this error:
PHP Fatal Error – yii\base\ErrorException
Class 'frontend\controllers\UploadedFile' not found
1. in C:\xampp\htdocs\forum\frontend\controllers\PostsController.php at line 70
61626364656667686970717273747576777879 * @return mixed
*/
public function actionCreate()
{
$model = new Posts();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//Get instance of the uploaded file
$imageName = $model->title;
$model->file = UploadedFile::getInstance($model,'file');
$model->file->saveAs('uploads/' .$imageName. '.' .$model->file->extension);
//Save the path in the db column
$model->logo = 'uploads/' .$imageName. '.' .$model->file->extension;
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
2. yii\base\ErrorHandler::handleFatalError()
Could you please tell me why is this happening?
Here is my full code:
VIEW
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use frontend\models\categories;
use yii\models\user;
use yii\web\IdentityInterface;
/* @var $this yii\web\View */
/* @var $model frontend\models\Posts */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="posts-form">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => 160]) ?>
<?= $form->field($model, 'posts')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'file')->fileInput() ?>
<?= $form->field($model, 'keywords')->textInput(['maxlength' => 45]) ?>
<?= $form->field($model, 'categories_id')->dropDownList(
ArrayHelper::map(Categories::find()->all(),'id','name'),
['prompt'=>'Select a category']) ?>
<?= $form->field($model, 'user_id')->textInput(['value' => \Yii::$app->user->identity->id]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
MODEL
<?php
namespace frontend\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
/**
* This is the model class for table "posts".
*
* @property integer $id
* @property string $title
* @property string $posts
* @property string $keywords
* @property string $created_at
* @property string $updated_at
* @property integer $categories_id
* @property integer $user_id
*
* @property Categories $categories
* @property User $user
*/
class Posts extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public $file;
public static function tableName()
{
return 'posts';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['title', 'posts', 'categories_id', 'user_id'], 'required'],
[['posts'], 'string'],
[['created_at', 'updated_at'], 'safe'],
[['categories_id', 'user_id'], 'integer'],
[['title'], 'string', 'max' => 160],
[['keywords'], 'string', 'max' => 45],
[['logo'], 'string', 'max' => 100],
[['file'], 'file'],
// [['categories'], 'string', 'max' => 64],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'title' => Yii::t('app', 'Title'),
'posts' => Yii::t('app', 'Posts'),
'keywords' => Yii::t('app', 'Keywords'),
'created_at' => Yii::t('app', 'Created At'),
'updated_at' => Yii::t('app', 'Updated At'),
'categories_id' => Yii::t('app', 'Categories ID'),
'user_id' => Yii::t('app', 'User ID'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCategories()
{
return $this->hasOne(Categories::className(), ['id' => 'categories_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
];
}
}
CONTROLLER
<?php
namespace frontend\controllers;
use Yii;
use frontend\models\Posts;
use frontend\models\PostsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadFile;
/**
* PostsController implements the CRUD actions for Posts model.
*/
class PostsController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Posts models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new PostsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Posts model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Posts model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Posts();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//Get instance of the uploaded file
$imageName = $model->title;
$model->file = UploadedFile::getInstance($model,'file');
$model->file->saveAs('uploads/' .$imageName. '.' .$model->file->extension);
//Save the path in the db column
$model->logo = 'uploads/' .$imageName. '.' .$model->file->extension;
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Posts model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Posts model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Posts model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Posts the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Posts::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
Thanks!
Ben