Kiryl Lapchynski
Kiryl Lapchynski

Reputation: 95

can't configure urls from backend to frontend Yii2

Web frontend - localhost (directory apache2/htdocs in the LAMP), web backend - localhost:8080/backend (apache2/htdocs/backend). Yii advanced application in the same directory with htdocs - mafia-yii (apache2/mafia-yii). File main.php in backend/config:

'components' => [
    ....
    'urlManager' => [
        // here is your normal backend url manager config
        'class' => 'yii\web\UrlManager',
        'baseUrl' => 'http://localhost/backend',
    ],
    'urlManagerFrontend' => [
        'class' => 'yii\web\UrlManager',
        'hostInfo' => 'http://localhost',
        'baseUrl' => 'http://localhost',
    ],

],

file ~/lampstack-7.0.4-0/apache2/mafia-yii/backend/views/layouts/main.php :

....
$menuItems[] = ['label' => 'Backend', 'url' => ['/site/index']];
$menuItems[] = ['label' => 'Fronend', 'url' => [Yii::$app->urlManagerFrontend->createUrl('/site/index')]];
....

Result: http://localhost:8080/backend/index.php?r=backend%2Findex.php%3Fr%3Dsite%252Findex

Not Found (#404)

Upvotes: 1

Views: 1218

Answers (3)

Kiryl Lapchynski
Kiryl Lapchynski

Reputation: 95

I solve the problem. Because:

$var[] = Yii::getAlias('@webroot');
$var[] = Yii::getAlias('@web');
\yii\helpers\VarDumper::dump($var);

returns: [ 0 => '/home/kira/lampstack-7.0.4-0/apache2/htdocs/backend' 1 => '/backend' ]

the solution is: $menuItems[] = ['label' => 'Frontend', 'url' => '@web/../'];

Upvotes: 0

Fabrizio Caldarelli
Fabrizio Caldarelli

Reputation: 3008

Have you tried with:

'components' => [
    ....
    'urlManager' => [
        // here is your normal backend url manager config
        'class' => 'yii\web\UrlManager',
        'baseUrl' => '/backend',
    ],
    'urlManagerFrontend' => [
        'class' => 'yii\web\UrlManager',
        'hostInfo' => 'http://localhost',
        'baseUrl' => '',
    ],

],

Upvotes: 1

Taras
Taras

Reputation: 582

Try to use the following code:

use yii\helpers\Url;

$menuItems[] = ['label' => 'Fronend', 'url' => Url::to(Yii::getAlias('@web') . '/site/index', true)];

Upvotes: 0

Related Questions