D
D
des1roer2015-06-25 09:04:19
Yii
des1roer, 2015-06-25 09:04:19

How to link to backend from frontend in Yii2?

How to add a link to the backend in the frontend menu?
UPD 1:
\yii2rbac\frontend\config\main.php

'components' => [
        'urlManager' => [
            //  'class' => 'backend\components\UrlManager',
            'enablePrettyUrl' => true,
        // 'rules' => require(__DIR__ . '/routes.php'),
        ],
        'urlManagerFrontend' => [
            'class' => 'yii\web\urlManager',
            'baseUrl' => 'http://localhost/yii2rbac',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        //'rules' => require(__DIR__ . '../frontend/routes.php'),
        ],

\yii2rbac\backend\config\main.php
'components' => [
        'urlManager' => [
            // 'class' => 'backend\components\UrlManager',
            'enablePrettyUrl' => true,
        // 'rules' => require(__DIR__ . '/routes.php'),
        ],

\yii2rbac\common\config\main.php
'components' => [
        'urlManager' => [
            'class' => 'yii\web\urlManager',
            'enablePrettyUrl' => true,
        ],

then in \yii2rbac\frontend\views\layouts\main.php
$menuItems = [
                ['label' => 'back', 'url' => Yii::$app->urlManagerFrontend->createUrl('backend/web/index.php'),],

and I get the link localhost/yii2rbac/backend/web/index.php
It seems to me that in order to get localhost/yii2rbac/frontend/web/index.php from the backend, I will have to mirror the same actions.
UPD 2:
I feel that I specify an absolute url.
It seems that through apache it can be somehow corrected. It is so?
Set document roots of your Web server:

For Apache:

<VirtualHost *:80>
    ServerName www.yii2-start.domain # You need to change it to your own domain  
    ServerAlias yii2-start.domain # You need to change it to your own domain  
    DocumentRoot /my/path/to/yii2-start # You need to change it to your own path  
    <Directory /my/path/to/yii2-start> # You need to change it to your own path  
        AllowOverride All  
    </Directory>  
</VirtualHost>

    Use the URL http://yii2-start.domain to access application frontend.
    Use the URL http://yii2-start.domain/backend/ to access application backend.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Mironyuk, 2015-06-25
@des1roer

In order to be able to form links to backend controllers in the frontend, you need to add a new UrlManager to the frontend.
In \yii2rbac\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 I put the routing rules 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.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question