3

I'm trying to link some controllers from frontend to backend. After some hours I don't where could be the problem.

Backend

file: main.php

    'urlManager' => [
        'enablePrettyUrl' => false,
        'showScriptName' => false,
        'baseUrl' => '/backend/web',
    ],        
    'urlManagerFrontEnd' => [
        'class' => 'yii\web\urlManager',
        'baseUrl' => '/frontend/web',
        'enablePrettyUrl' => false,
        'showScriptName' => false,
    ]


file: SiteController.php

    public function actionIndex()
    {
        // User's variable
        $user = \common\models\User::findIdentity(Yii::$app->user->id);

        if($user->role != self::USER_ADMIN){
            return $this->redirect(Url::to(Yii::$app->urlManagerFrontEnd->createUrl(['/site/index'])));
        }

        return $this->render('index');
    }

Using this

Url::to(Yii::$app->urlManagerFrontEnd->createUrl(['/site/index']))

Returns me

/advanced/backend/web/index.php?r=site%2Findex

Any advice?

Dorin Brage
  • 158
  • 1
  • 5
  • 14

5 Answers5

0

Your code is correct. urlManagerFrontEnd should return url based on baseUrl /frontend/web.

Try change baseUrl to http://yourdomain/

Onedev_Link
  • 1,981
  • 13
  • 26
  • I gues I have the problem because is hosted in localhost so, finallyI solved it in this way. ` const ROLE_ADMIN = 0; public function beforeAction($event) { // User's variable $user = \common\models\User::findIdentity(Yii::$app->user->id); if (is_null($user) == false) { if ($user->role == self::ROLE_ADMIN) { return $this->redirect(["site/logout"]); } } return parent::beforeAction($event); } ` – Dorin Brage Sep 10 '15 at 10:30
0

I did little googling and found this link.

for reference I am posting same here

I read around in the UrlManager.php and found the following:

$baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();

So this means when showScriptName= true and enablePrettyUrl=false $baseUrl = getScriptUrl() otherwise $baseUrl = getBaseUrl()

So it just work with prettyUrl=true and the showScriptName = false. When we set prettyUrl on true it takes $baseUrl = getBaseUrl() Changing it to the following it resolves our problem =).

/*$baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();*/

$baseUrl = !$this->showScriptName || $this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();

Now you have to set prettyurl=false and the other on true et voila

I tried this on a fresh template and then applied code you mentioned in question and got the same error as you got.

But then after the fix I did according to this post I get correct path.

This link is also helpful.

ankitr
  • 5,992
  • 7
  • 47
  • 66
0

in your frontend config add this to the top to define 2 variables.

use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '/frontend/web', (new Request)->getBaseUrl());
$backEndBaseUrl = str_replace('/frontend/web', '/backend/web', (new Request)->getBaseUrl());

And set these variables as the baseUrl parameters in the components

'components' => [
    'urlManager' => [
        'class' => 'yii\web\urlManager',
        'enablePrettyUrl' => false,
        'showScriptName' => false,
        //'baseUrl' => '/frontend/web',
        'baseUrl'=> $baseUrl,
    ],
    'urlManagerBackEnd' => [
        'class' => 'yii\web\urlManager',
        'enablePrettyUrl' => false,
        'showScriptName' => false,
        //'baseUrl' => '/backend/web',
        'baseUrl' => $backEndBaseUrl,
    ],

then you can have links from the frontend to the backend by e.g.

$backendUrl= Yii::$app->urlManagerBackEnd->createUrl('//');
echo yii\helpers\Html::a('link to backend', $backendUrl);

to have the same from the backend to the frontend add this to the backend config:

use \yii\web\Request;
$baseUrl = str_replace('/backend/web', '/backend/web', (new Request)->getBaseUrl());
$frontEndBaseUrl = str_replace('/backend/web', '/frontend/web', (new Request)->getBaseUrl());

and in the components:

'urlManager' => [
        'class' => 'yii\web\urlManager',
        'enablePrettyUrl' => false,
        'showScriptName' => false,
        'baseUrl'=> $baseUrl,
    ],
    'urlManagerFrontEnd' => [
        'class' => 'yii\web\urlManager',
        'enablePrettyUrl' => false,
        'showScriptName' => false,
        //'baseUrl' => '/backend/web',
        'baseUrl' => $frontEndBaseUrl,
    ],

and to create links use:

$frontendUrl= Yii::$app->urlManagerFrontEnd->createUrl('//');
echo yii\helpers\Html::a('link to frontend', $frontendUrl);

forgot you can of course also link to specific pages e.g. from backend to frontend site/about:

$frontendUrl= Yii::$app->urlManagerFrontEnd->createUrl('/site/about');
echo yii\helpers\Html::a('link to frontend site about', $frontendUrl);

BTW. if you have removed the /web behavior by some htaccess you should also remove it in the variables.

BHoft
  • 1,663
  • 11
  • 18
0

use this code. it will redirect you to front end

return $this->redirect(Yii::$app->urlManager->createUrl('./../../frontend/web/'));
Kalyan Halder
  • 1,485
  • 24
  • 28
0

Use below one:

Url::to(Yii::$app->urlManagerBackEnd->createUrl('index.php/'/site/index'), true);

Oomph Fortuity
  • 5,710
  • 10
  • 44
  • 89
GaganS
  • 1
  • 1