M
M
microf2018-09-22 16:04:05
Yii
microf, 2018-09-22 16:04:05

YII2: How to get module settings in controller?

Hello. I'm making a file upload module. I want to make the save folder customizable when the module is connected:

'modules' => [
     
        'image' => [
            'class' => 'common\modules\image\Image',           
            'folder_image' => '/uploads/image_news',
            'folder_image_preview' => '/uploads/image_news_preview',
        ],
        ],

In common/modules/image/Image.phpI specify these "variables"
public $folder_image;
 public $folder_image_preview;

And how can I get them in the module controller or module model?
So that you can substitute them, for example, here, instead of /web/uploads
$file->saveAs(Yii::getAlias('@frontend') . '/web/uploads/' . $filename);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-09-22
@microf

Good afternoon.
If you specify a property in the model, then you need to access it like this:

public $filename;

$model = new Image();
$model->filename;

If you want to specify it as a module parameter, then specify it in the module class. Or in the config file as a parameter
'main' => [
            'class' => 'app\modules\main\MainModule',
            'layout' => 'main.php',
            'layoutPath' => '@app/modules/main/views/layouts',
            'params' => [
                'test' => 'test1',
                'test2' => 'test22'
            ]
        ],

Then you can apply like this:
// Если указали в классе модуля public $filename
$main = Yii::$app->getModule('main');
echo $main->filename;

Or like this:
// Если указали параметром в конфигурационном файле.
$main = Yii::$app->getModule('main');
echo $main->params['test'];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question