B
B
balex7772019-10-21 10:44:46
Yii
balex777, 2019-10-21 10:44:46

How to get yii\web\Application Object(Yii:$app) frontend from backend in yii2?

The controller is in the backend.
Yii:$app only returns data about the application from the backend, but you also need the frontend to get the controllers that are there.
How to get frontend application data from backend?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2019-10-21
@balex777

Yii:$app->controllergets the current controller. It doesn't matter where you are. Most likely you meant routing between frontend and backend .
In order to be able to form links to backend controllers in frontend , you need to add a new urlManager in frontend In \progect\frontend\config\main.php add the following component

'urlManagerBackend' => [
            'class' => 'yii\web\urlManager',
            'baseUrl' => 'http://yoursitedomain.ru',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => require(__DIR__ . '../backend/routes.php'),
        ],

After that you can easily create backend links from frontend using the second manager
Please note that the routing rules are placed in a separate file. these rules will be used in both urlManager backend and urlManagerBackend in frontend . This saves us from duplication.
Similarly, you can configure the formation of links to the frontend from the backend
. If you need to get all the routes in the system, then you can see how this is implemented in the popular RBAC extension

L
Lander, 2019-10-21
@usdglander

You are obviously doing something wrong... In principle, the situation when you need to pull another from one controller is a violation of the architecture. And the data is obtained through models. If the model is used only in the frontend or backend, then it must be in the appropriate directory. If it's a generic model, then use common.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question