3
votes

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?

5
@InsaneSkulll When I try to redirect to the Frontend, Yii says me Page not found . I'm stucked and I'd like to know how I could fix itDorin Brage
You don't need Url::to()soju
look at my solution to fix your problem.BHoft
Hello @JorgeBrage, please post your solution as an answer.Vinaya Maheshwari

5 Answers

0
votes

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

Try change baseUrl to http://yourdomain/

0
votes

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.

0
votes

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.

0
votes

use this code. it will redirect you to front end

return $this->redirect(Yii::$app->urlManager->createUrl('./../../frontend/web/'));
0
votes

Use below one:

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