SaidbakR SaidbakR - 1 year ago 133
PHP Question

Yii2 check if a package is defined or not

In another Yii2 application I used a package called

to replace the
with HTMl editable box. In the current application I don't want to use it while I want to keep everything easy reusable. In other words, I want a conditional check says that, if the package is installed call it, if not call the ordinary active form textarea.

I have tried
like the following:

// _form.php code
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
use navatech\roxymce\widgets\RoxyMceWidget;
<?php if (class_exists('RoxyMceWidget')): ?>
<?= RoxyMceWidget::widget([
'model' => $model, //your Model, REQUIRED
'attribute' => 'content', //attribute name of your model, REQUIRED if using 'model' section
'name' => 'Post[content]', //default name of textarea which will be auto generated, NOT REQUIRED if using 'model' section
'value' => isset($_POST['Post']['content']) ? $_POST['Post']['content'] : $model->content, //default value of current textarea, NOT REQUIRED
'action' => Url::to(['roxymce/default']), //default roxymce action route, NOT REQUIRED
'options' => [//TinyMce options, NOT REQUIRED, see
'title' => 'RoxyMCE',//title of roxymce dialog, NOT REQUIRED
'height' => 450,

<?php else: ?>
<?= $form->field($model, 'content')->textarea(['rows' => 14]);?>
<?php endif; ?>

However, after the installation of
using composer, the conditional statement gives the same result. i.e printing the ordinary activeform text area, so
seems to always return false inspite off the widget is being installed.

Is there any other right way to check if a package is found or not?

Answer Source

You must provide fully qualified namespace for the class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download