i have the question about how to call the Controller for View. I want to do upload my Picture to my host. i created the model upload with the name upload.php and put in model folder. like this "\protected\models"
<?php class Upload extends CActiveRecord { public $image; // ... other attributes public function rules() { return array( array('image', 'file', 'types'=>'jpg, gif, png', 'safe' => false), ); } }
then i made the Controller with the name UploadController.php and put in Controller folder. "\controllers\admin".
<?php class UploadController extends Controller
{
public $layout='//content';
public function actionCreate()
{
$model=new Upload;
if(isset($_POST['Upload']))
{
$model->attributes=$_POST['Upload'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->image->saveAs('/../themes/default/images/shopimg');
// redirect to success page
}
}
$this->render('/admin/upload', array('model'=>$model));
}
}
then made the view like this with name upload.php and put it in view folder
<div class="note">
<div class="note_title">
<a href="<?php echo Yii::app()->homeUrl; ?>admin">Admin area</a> → Add Picture
</div>
<div class="note_body">
<?php if(Yii::app()->user->hasFlash('message')) echo Yii::app()->user->getFlash('message'); ?>
<?php $form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);
?>
<div class="form"><?php echo $form->errorSummary($model); ?></div>
<table class="table_info">
<tr>
<td><?php echo $form->labelEx($model, 'image');?></td>
<td><?php echo $form->fileField($model, 'image');?></td>
<td><?php echo $form->error($model, 'image');?></td>
<td><?php echo CHtml::submitButton('Submit');?></td>
</tr>
</table>
<?php $this->endWidget(); ?>
</div>
</div>
then in my main page main.php i call that upload action with this code
<td>
<a href="<?php echo Yii::app()->homeUrl; ?>admin/upload"><p>Items Upload</p>
Items Upload</a>
</td>
but it not working i don't know what i missing ! i alway said "Error 404 Unable to resolve the request "upload". " what that mean ?
</div>
In yii url format will be based on controller and action.
Ex:- Controller name is UploadController and action is Create then url should be upload/create
<a href="<?php echo Yii::app()->homeUrl; ?>upload/create"><p>Items Upload</p>Items Upload</a>
if your controller is inside any module then url should be module_name/controller_name/action_name.