SaidbakR SaidbakR - 5 days ago 5
PHP Question

Yii2 check if a package is defined or not

In another Yii2 application I used a package called

navatech/yii2-roxymce
to replace the
textarea
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
class_exists
like the following:

<?php
// _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 https://www.tinymce.com/docs/
'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
navatech\roxymce\widgets\RoxyMceWidget
using composer, the conditional statement gives the same result. i.e printing the ordinary activeform text area, so
class_exists
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

You must provide fully qualified namespace for the class.

class_exists('navatech\roxymce\widgets\RoxyMceWidget')
Comments