1

I'm using Yii2 advanced, which have frontend and backend, I'm using two domain to access frontend and backend respectively.

For some reason, I add a baseUrl(/admin) to backend, which will cause links like http://backend.example.com/controller/action change to backend.example.com/admin/controller/action, after this changing, the page can be load correctly

'components' => [
    'request' => [
        'csrfParam' => '_csrf-backend',
        //All requests will add "/admin",e.g:
        //backend.example.com/controller/action will change to
        //backend.example.com/admin/controller/action
        'baseUrl' => '/admin',
    ],
],

But it caused another problem: assets can't load cause it add /admin too

enter image description here

I tried changing the baseUrl of AppAssets in backend/assets/AppAsset.php, but not working

<?php

namespace backend\assets;

use yii\web\AssetBundle;

/**
 * Main backend application asset bundle.
 */
class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
    ];
    public $js = [
    
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

It acts like $baseUrl doesn't exists, no matter what value I set to $baseUrl, the page just keep requesting /admin/assets/86321cf/xxxx.js, how can I remove the /admin by modifying settings in backend/assets/AppAsset.php?

If I use the registerJsFile() method to register js to the page

$this->registerJsFile('plugins/ImageViewer/imageviewer.min.js', ['position' => View::POS_END, 'depends'=>JqueryAsset::class]);

it's loading the correct path(which means not adding /admin to the path) enter image description here

So I was wondering how can I let the page load correct path of assets by changing configurations of backend/assets/AppAsset.php or can I solve it by changing configurations in other file?

Willis
  • 599
  • 3
  • 25

2 Answers2

0

$baseUrl in asset - specifies the URL corresponding to the directory basePath. Like basePath, if you specify the sourcePath property, the asset manager will publish the assets and overwrite this property accordingly. try to write @app/backend/web in baseUrl since it seems to me that @web after being assigned to the request will be equal to / admin

  • Thank you, but in my case, the `$baseUrl` in `AppAssets` totally not working after I set the request baseUrl in `main.php`, no matter what value I set to it, it acts just like doesn't have this property at all. I'm using Yii2 Advaced `2.0.38`. – Willis Oct 26 '20 at 03:36
0

OK, after massive searching and asking people, I solve this myself, it turns out that we can set the $baseUrl of backend/assets/AppAsset.php in main.php in this way

'components' => [
    'assetManager' => [
        //after settting components.request.baseUrl='/admin', the assets url will automatically add "/admin" to its url
        //to avoid this, we can set the asset baseUrl in components.assetManager.baseUrl, set it to '/assets'
        'baseUrl' => '/assets',
    ],
    'request' => [
        'csrfParam' => '_csrf-backend',
        //All requests will add "/admin",e.g:
        //www.example.com/controller/action will change to
        //www.example.com/admin/controller/action
        'baseUrl' => '/admin',
    ],
    // other configs....
],

These two link inspired me:
Yii - Assets
Override Yii2 assetManager config in controller

Willis
  • 599
  • 3
  • 25