L
L
Lander2015-06-11 10:21:00
Yii
Lander, 2015-06-11 10:21:00

How to call a controller method whose name is in a variable?

Yii2. The bottom line is that I want to get the names of the controllers from the database and call one method from them. I try:

public function actionIndex()
{
    $plugin = 'FilesController';
    $data['files'] = $plugin::thumbnail();
    return $this->render('index', ['data' => $data]);
}

I get: Class 'FilesController' not found .
Although:
public function actionIndex()
{
    $data['files'] = FilesController::thumbnail();
    return $this->render('index', ['data' => $data]);
}

works. What am I doing wrong?
upd: Of course, I solved the problem by specifying the class name along with the namespace, but I feel that this is not the true way
$plugin = 'app\modules\cabinet\controllers\FilesController';
    $data['files'] = $plugin::thumbnail();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cloud_zurbag, 2015-06-11
@usdglander

Why not do so?

$plugin = __NAMESPACE__ . '/FilesController';
$data['files'] = $plugin::thumbnail();

Or
$plugin = __NAMESPACE__ . '/FilesController';
$data['files'] = call_user_func([$plugin, 'thumbnail']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question