A
A
agent11562017-01-06 14:23:35
Yii
agent1156, 2017-01-06 14:23:35

How to write a link?

['label' => 'About', 'url' => ['/site/about']]
how to write here to go from frontend to backend ????

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-01-06
@agent1156

frontend and backend are separate app. And the url manager of one app is unaware of the existence of the second. Therefore, you need to make another urlManager in the frontend config, which will know about the backend. And use it when generating links.
For example:

//это стандарный 
        'urlManager' => [
            'class' => 'yii\web\urlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [],
        ],
//это второй 
        'urlManagerBack' => [
            'class' => 'yii\web\urlManager',
            'baseUrl' => 'http://mybackenddomain.com',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => require(__DIR__ . '/../../backend/config/routes.php'), //тут путь к файлу с rules для backend
        ],

And create a link like this:
Yii::$app->urlManagerBack->createAbsoluteUrl('/'); //ссылка на главную
Yii::$app->urlManagerBack->createUrl(['site/about']); //Ваш пример

A
agent1156, 2017-01-06
@agent1156

front end

<?php
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',
    'components' => [


        'urlManagerBack' => [

            'class' => 'yii\web\UrlManager',
            'baseUrl' => 'localhost/fashion-house',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => require(__DIR__ . '/../../backend/config/main.php'), //тут путь к файлу с rules для backend

        ],


        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],

        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [


            ],
        ],



    ],



    'params' => $params,
];

backend
<?php
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [
    'id' => 'app-backend',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'backend\controllers',
    'bootstrap' => ['log'],
    'modules' => [],
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],
        
    ],
    'params' => $params,
];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question