add UploadController 'rest' controller

This commit is contained in:
rocho 2015-11-27 15:27:46 +01:00
parent 4e82c8ce71
commit 40509961cd
7 changed files with 111 additions and 3 deletions

3
.gitignore vendored
View File

@ -30,3 +30,6 @@ phpunit.phar
/phpunit.xml /phpunit.xml
/node_modules /node_modules
/frontend/web/profile/**
!/frontend/web/profile/.gitkeep

View File

@ -6,6 +6,7 @@ use yii\filters\AccessControl;
use yii\web\Controller; use yii\web\Controller;
use common\models\LoginForm; use common\models\LoginForm;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
use backend\models\UploadForm;
/** /**
* Site controller * Site controller
@ -22,7 +23,7 @@ class SiteController extends Controller
'class' => AccessControl::className(), 'class' => AccessControl::className(),
'rules' => [ 'rules' => [
[ [
'actions' => ['login', 'error'], 'actions' => ['login', 'error','upload-image'],
'allow' => true, 'allow' => true,
], ],
[ [
@ -55,7 +56,6 @@ class SiteController extends Controller
public function actionIndex() public function actionIndex()
{ {
Yii::$app->security->generatePasswordHash('test');
return $this->render('index'); return $this->render('index');
} }
@ -85,4 +85,11 @@ class SiteController extends Controller
return $this->goHome(); return $this->goHome();
} }
public function actionUploadImage(){
\yii::$app->request->enableCsrfValidation = false;
$model = new UploadForm();
return $this->render('upload', ['model' =>$model]);
}
} }

View File

@ -0,0 +1,46 @@
<?php
namespace backend\controllers;
use yii\rest\ActiveController;
use yii\rest\Controller;
class UploadController extends Controller
{
// public function verbs()
// {
// $verbs = [];
// $verbs[ "upload" ] = ['POST' ];
// return $verbs;
// }
public function actionUpload()
{
$postdata = fopen( $_FILES[ 'data' ][ 'tmp_name' ], "r" );
// /* Get file extension */
$extension = substr( $_FILES[ 'data' ][ 'name' ], strrpos( $_FILES[ 'data' ][ 'name' ], '.' ) );
// /* Generate unique name */
$filename = \Yii::getAlias('@frontend') ."/web/profile/" . uniqid() . $extension;
echo $filename;
// /* Open a file for writing */
$fp = fopen( $filename, "w" );
/* Read the data 1 KB at a time
and write to the file */
while( $data = fread( $postdata, 1024 ) )
fwrite( $fp, $data );
/* Close the streams */
fclose( $fp );
fclose( $postdata );
/* the result object that is sent to client*/
echo "ok";
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace backend\models;
use yii\base\Model;
use common\models\User;
class UploadForm extends Model{
public $image ;
/**
* @inheritdoc
* @formatter:off
*/
public function rules()
{
return [['image',function ($attribute, $params) {}], ];
}
public function save(){
return true;
}
}

View File

@ -21,6 +21,7 @@ use yii\helpers\Html;
<div class="navbar-custom-menu"> <div class="navbar-custom-menu">
<?php if ( !Yii::$app->user->isGuest ){?>
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<!-- User Account: style can be found in dropdown.less --> <!-- User Account: style can be found in dropdown.less -->
@ -63,6 +64,9 @@ use yii\helpers\Html;
<a href="#" data-toggle="control-sidebar"><i class="fa fa-gears"></i></a> <a href="#" data-toggle="control-sidebar"><i class="fa fa-gears"></i></a>
</li> </li>
--> -->
<?php
}
?>
</ul> </ul>
</div> </div>
</nav> </nav>

View File

@ -0,0 +1,24 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use backend\models\UploadForm;
/* @var $this yii\web\View */
/* @var $model common\models\Ticket */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="ticket-form">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<div class="form-group">
<?= Html::submitButton( Yii::t('common/ticket', 'Create') ,['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File