Как сделать saveAs() объектом?
Помогите пожалуйста чайнику!
<?php
Файл - SiteController.php
namespace app\controllers;
use Yii;
use yii\helpers\Html;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\UploadedFile;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\MyForm;
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Displays homepage.
*
*
return string
*/
public function actionIndex()
{
return $this->render('index');
}
/**
* Login action.
*
*
return string
*/
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
return $this->render('login', [
'model' => $model,
]);
}
/**
* Logout action.
*
*
return string
*/
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
/**
* Displays contact page.
*
*
return string
*/
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
/**
* Displays about page.
*
*
return string
*/
public function actionAbout()
{
return $this->render('about');
}
public function actionHello($message = 'Hello World!'){
return $this->render('hello', ['message' => $message]);
}
public function actionForm(){
$form = new MyForm();
if($form->load(Yii::$app->request->post()) && $form->validate()){
$name = Html::encode($form->name);
$email = Html:: encode($form->email);
$form->file = UploadedFile::getInstance($form, 'file');
$form->file->saveAs('photo/'.$form->file->baseName.$form->file->extension);
}
else {
$name = '';
$email = '';
}
return $this->render('form',
['form' => $form,
'name' => $name,
'email' => $email]);
}
}
Файл - MyFopm.php
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
class MyForm extends Model
{
public $name;
public $email;
public $file;
public function rules() {
return[
[['name', 'email'], 'required', 'message' => 'Вы не заполнили поле!'],
['email', 'email', 'message' => 'Некорректный e-mail адрес!'],
[['file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
];
}
Файл - ContactForm.php
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
/**
*
return array the validation rules.
*/
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],
];
}
/**
*
return array customized attribute labels
*/
public function attributeLabels()
{
return [
'verifyCode' => 'Verification Code',
];
}
/**
* Sends an email to the specified email address using the information collected by this model.
* @param string $email the target email address
*
return boolean whether the model passes validation
*/
public function contact($email)
{
if ($this->validate()) {
Yii::$app->mailer->compose()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
return true;
}
return false;
}
}