Creating links from backend to frontend and frontend to backend

I have a problem with links from backend to frontend and frontend to backend, which are working differently.

frontend\config\main.php


<?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' => [

		'session' => [

			'name' => 'PHPGESESSID',

			'savePath' => __DIR__ . '/../tmp',

		],

        'user' => [

			'enableSession' => true,

            'identityClass' => 'common\models\User',

            'enableAutoLogin' => false,

        ],

        'log' => [

            'traceLevel' => YII_DEBUG ? 3 : 0,

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                ],

            ],

        ],

        'errorHandler' => [

            'errorAction' => 'site/error',

        ],

		'urlManager'=>[

			'enablePrettyUrl' => false,

			'class' => 'yii\web\UrlManager',

			'hostInfo' => 'http://localhost/frontend',

		],

		'urlManagerBackend'=>[

			'enablePrettyUrl' => false,

			'class' => 'yii\web\UrlManager',

			'hostInfo' => 'http://localhost/backend',

			'baseUrl' => 'http://localhost/backend',

		],

    ],

    'params' => $params,

];



For frontend, the links in the NAVBAR views are:


            $menuItems = [

                ['label' => 'Home', 'url' => ['/site/index']],

                ['label' => 'About', 'url' => ['/site/about']],

                ['label' => 'Contact', 'url' => ['/site/contact']],

		['label' => 'Sign in', 'url' => Yii::$app->urlManagerBackend->createUrl('site/login')],

            ];



The href we get are respectively,

localhost/frontend/site/index

localhost/frontend/site/about

localhost/frontend/site/contact

[color="#FF0000"]localhost/frontend/site/login[/color]

Captcha is working in the contact form in the frontend

Now, backend

backend\config\main.php


<?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',

    'language' => 'en-US',

    'bootstrap' => ['log'],

    'modules' => [],

    'components' => [

        'session' => [

			'name' => 'PHPADSESSID',

			'savePath' => __DIR__ . '/../tmp',

		],

		'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' => false,

			'class' => 'yii\web\UrlManager',

			'hostInfo' => 'http://localhost/backend',

		],

		'urlManagerFrontend'=>[

			'enablePrettyUrl' => true,

			'class' => 'yii\web\UrlManager',

			'showScriptName'=>false,

			'suffix' => '.html',

			'hostInfo' => 'http://localhost/frontend',

			'baseUrl' => 'http://localhost/frontend',

		],

		'i18n' => [

			'translations' => [

				'app*' => [

					'class' => 'yii\i18n\PhpMessageSource',

					'basePath' => '@common/messages',

					'sourceLanguage' => 'en-US',

				],

			],

		],

	],

	'params' => $params,

];



For backend, the links in the NAVBAR views are:


            $menuItems = [

			['label' => \Yii::t('appmsg', 'Register'), 'url' => ['/site/register']],

			['label' => \Yii::t('appmsg', 'Subscribe'), 'url' => Yii::$app->urlManagerFrontend->createUrl('site/subscribe'),],

			['label' => \Yii::t('appmsg', 'Contact us'), 'url' => Yii::$app->urlManagerFrontend->createUrl('site/contact'),],

            ];



The href we get are respectively,

localhost/backend/site/register

[color="#2E8B57"]localhost/frontend/site/subscribe

localhost/frontend/site/contact[/color]

[color="#FF0000"]Captcha is NOT working in the register form in the frontend, even though the model, action and view are made similar to localhost/frontend/site/contact

[/color]

I tried to add ‘request’ component with ‘baseUrl’ but results are not different. I seek some advice to guide me to find my flaw.

thanks in advance.

1 Like

I could figure out why backend and frontend behave differently,

In the backend, the link to the frontend works if:


		'urlManagerFrontend'=>[

			'enablePrettyUrl' => true,

			'class' => 'yii\web\UrlManager',

			'showScriptName'=>false,

			'suffix' => '.html',

			'hostInfo' => 'http://localhost/frontend',

			'baseUrl' => 'http://localhost/frontend',

		],



But, do not work and point to the similar route in backend if:


		'urlManagerFrontend'=>[

			'enablePrettyUrl' => false,

			'class' => 'yii\web\UrlManager',

			//'showScriptName'=>false,

			//'suffix' => '.html',

			'hostInfo' => 'http://localhost/frontend',

			'baseUrl' => 'http://localhost/frontend',

		],



Also, I am in a weird situation where I cannot have ‘enablePrettyUrl’=>true for frontend and backend together.

Because, I am developing in WT-NMP which give nginx in windows.

If I point the root to either frontend or backend as ‘localhost’, enablePrettyUrl=>true works, else I get a 404 error. The other one, I can point only as ‘localhost/admin’ or similar, where enablePrettyUlr=>true do not work.

I look forward to some suggestions. thanks.

Hi I have the same error. That it just works with the prettyurl stuff on true.

I just handled it at this time with a dirty solution:


        'urlManager' => [

            // here is your frontend URL rules

        ],

        'urlManagerFrontEnd' => [

            'class' => 'yii\web\urlManager',

            'baseUrl' => '/counselis/frontend/web',

            'enablePrettyUrl' => false,

            'showScriptName' => false,

            'rules' => [

                ['?r=site%2Fconcept' => '/site/concept']

            ],

        ],

Ignore the rules it just was a try but I couldn’t complete this.

and in your layout navigation:


['label' => Yii::t('lang', 'About'), 'url' => \Yii::$app->urlManagerFrontEnd->baseUrl . '/index.php?r=site%2Fabout'],

It is not pretty but I did not find another solution untill now

This is another post from me, where I asked something equal. advanced template frontend backend url specific

Quite interesting is that it worked with the following from backend to frontend:


['label' => 'Backend', 'url' => \Yii::$app->urlManagerBackEnd->baseUrl]

Thanks for your reply. Your idea works.

But still it does not solve my problem. I wanted the links from frontend to backend and from backend to frontend working when the enablePrettyUlr=>false. There I am still stuck.

I have the site done in basic, which works, but I thought I could use https in backend and give different session, cookie etc. Now, to change each and every url, will be a trouble for me, and in the future too to maintain.

But you gave me a working idea, thanks for that.

I repeated the same in XAMPP local host instead of WT-NMP, there is no change.

  1. Backend pretty url not working

  2. Backend Captcha not working

Maybe this is something for github issue when the behavior changes with an option?

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

Ask again if you have trouble I will forward it to github later

Thank you very much. You found the problem. I went through UrlManager a couple of times but could not identify this. You are the man.

By adding ‘scriptUrl’ in the ‘urlManagerBackend’ in the frontend\config\main.php solves my cross-linking problem.


		'urlManagerBackend'=>[

			'enablePrettyUrl' => false,

			'class' => 'yii\web\UrlManager',

			'scriptUrl' => '/admin',

			'baseUrl' => 'http://localhost/admin',

		],



Still, Captcha is not working for me in the backend forms. Is it only for me?

One more problem while implementing this solution.

If this above solution is implemented, Verbfilter is not recognizing post as post.




    public function behaviors()

    {

        return [

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'logout' => ['post'],

                    'language' => ['post'],

                ],

            ],

        ];

    }



We get ‘Method Not Allowed. This url can only handle the following request methods: POST’ 404 Error

If we remove ‘scriptUrl’ from the urlManager component, things are again OK.

EDIT

This happened when ‘scriptUrl’ was added in the ‘urlManager’ component of backend, which is not required. The solution still works. The ‘scriptUrl’ needs to be added in the ‘urlManagerBackend’ component of ‘frontend/config/main.php’ only.