1

我是yii2的新手。我想在我的项目中使用roxymce文件管理器。yii2我按照这些文档使用yii但在使用本节Undefined variable: form出现错误,在使用时ActiveForm::begin()出现Getting unknown property: navatech\roxymce\models\UploadForm::thumb错误。我想知道什么时候必须在我的项目中使用那个控制器。我认为此代码fileupload

<?php

use yii\widgets\ActiveForm;
use yii\web\View;
use yii\helpers\Url;


$this->title = Yii::t('app', 'Upload Course Files');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Presented Courses'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="row">
    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'], 'action' => '#']); ?>
    <?php $form->field($model, 'file')->fileInput(['id' => 'fieldID1'])->label(false) ?>
    <a href="<?=Url::to([
        '/roxymce/default',
        'type' => 'media',
        'input' => 'fieldID1',
        'dialog' => 'iframe',
    ]) ?>" id="fileup" class="fancybox" ><i class="fa fa-upload"></i></a>
    <?php ActiveForm::end();?>


<?php
$this->registerJsFile('@web/js/jquery.fancybox.min.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerCssFile('@web/css/jquery.fancybox.min.css', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJs(' 
$("#fileup").fancybox({  type: "iframe"});  
', View::POS_END);
?>

这段代码是针对这个 view( fileupload) 的控制器的:

public function actionUploadfile()
{
    $model = new UploadForm();
    return $this->render('uploadfile',['model'=>$model]);
}

这是我在后端目录中的主要配置文件:

'modules' => [

        'roxymce' => [
            'class' => 'navatech\roxymce\Module',
            'uploadFolder' => '@app/web/uploads/images',
            'uploadUrl' => '/uploads/images',
        ],

    ],

我使用了 yii2 高级模板。如果有人使用此模块,请提示我。

4

1 回答 1

0

$form只是因为如果您使用示例中的代码,则未定义变量。在您看来,当您使用扩展时,您需要ActiveForm::begin()像这样使用:

简单的例子

<?php 

$form = ActiveForm::begin(['id' => 'roxymce-form']); 

echo $form->field($model, 'thumb')->textInput(['id' => 'fieldID'])->label(false); 

?>

<a href="<?= \yii\helpers\Url::to([
    '/roxymce/default',
    'type'   => 'image',
    'input'  => 'fieldID',
    'dialog' => 'fancybox',
]) ?>"><i class="fa fa-upload"></i></a>

<script>
    $("a").fancybox();
</script>

<?php ActiveForm::end(); ?>

[编辑]

运行它非常困难,但最终我成功了。我使用 Yii2 的基本模板。从指南中的安装包开始composer。在此之后创建资产包的类以发布 javascript 包。我的资产包是这些,它们位于applicationroot/assets文件夹中:

资产/AppAsset.php

<?php 
namespace app\assets;

use yii\web\AssetBundle;

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
    public $jsOptions = [
        'position' => \yii\web\View::POS_HEAD,
    ];
}

资产/FancyBoxAsset.php

<?php
namespace app\assets;

use yii\web\AssetBundle;

class FancyBoxAsset extends AssetBundle
{
    public $sourcePath = '@bower/fancybox/source';

    public $js = [
        'jquery.fancybox.pack.js',
    ];
}

资产/FontAwesomeAsset.php

<?php
namespace app\assets;

use yii\web\AssetBundle;

class FontAwesomeAsset extends AssetBundle
{
    public $sourcePath = '@bower/fontawesome';
    public $css = [
        'css/font-awesome.min.css',
    ];
}

资产/LazyLoadAsset.php

<?php
namespace app\assets;

use yii\web\AssetBundle;

class LazyLoadAsset extends AssetBundle
{
    public $sourcePath = '@bower/jquery.lazyload';
}

资产/PatternflyTreeviewAsset.php

<?php
namespace app\assets;

use yii\web\AssetBundle;

class PatternflyTreeviewAsset extends AssetBundle
{
    public $sourcePath = '@bower/patternfly-bootstrap-treeview/dist';
    public $css = [
        'bootstrap-treeview.css',
    ];
    public $js = [
        'bootstrap-treeview.js',
    ];
}

资产/TinymceAsset.php

<?php

namespace app\assets;

use yii\web\AssetBundle;

class TinymceAsset extends AssetBundle
{
    public $sourcePath = '@bower/tinymce';
    public $jsOptions = [
        'position' => \yii\web\View::POS_HEAD,
    ];
}

现在您必须在配置文件中添加以下代码行:

配置/web.php

return [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    ...
    'modules' => [
        'roxymce' => [
            'class' => 'navatech\roxymce\Module',
            'uploadFolder' => '@app/web/uploads/images',
            'uploadUrl' => '../uploads/images',
        ],
    ],
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '' => 'site/index',
            '<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
            '<module:\w+>/<controller:\w+>/<action:\w+>/' => '<module>/<controller>/<action>',
        ],
    ]
    ...
];

在您需要将应用程序配置为在干净的 URL 上下文中工作之后。关注此以获得更多信息: 在 Yii2 中启用干净的 URL

现在您终于可以在所有上下文中完全使用该插件了。有两种使用方法:

  1. 与 TinyMce 集成

意见/网站/tinymceIntegrated.php

<?php
use \app\assets;

assets\FontAwesomeAsset::register($this);
assets\LazyLoadAsset::register($this);
assets\FancyBoxAsset::register($this);
assets\PatternflyTreeviewAsset::register($this);
assets\TinymceAsset::register($this);

// Include ActiveRecord Model
echo \navatech\roxymce\widgets\RoxyMceWidget::widget([
    'model'     => app\models\YourModel::findOne(1),
    'attribute' => 'content',
]);

// Sample HTML without ActiveRecord Model
echo \navatech\roxymce\widgets\RoxyMceWidget::widget([
    'name' => 'Post[content]',
]);
  1. 没有 TinyMce

意见/网站/tinymceWithout.php

<?php
use yii\helpers\Html;
use \app\assets;

assets\FontAwesomeAsset::register($this);
assets\LazyLoadAsset::register($this);
assets\FancyBoxAsset::register($this);
assets\PatternflyTreeviewAsset::register($this);
//assets\TinymceAsset::register($this);

$js = <<<JS
    $("a").fancybox();
JS;
$this->registerJs($js, \yii\web\View::POS_READY, 'upload-handler');
?>       
<a href="<?= \yii\helpers\Url::to([
    '/roxymce/default',
    'type' => 'image',
    'dialog' => 'fancybox',
]) ?>"><i class="fa fa-upload"></i></a>

使用此配置为我工作。

于 2017-11-28T08:39:38.483 回答