Virtual attribute with asset->baseUrl in controller: howto?

I am trying to make a virtual attribute with the published assetpath in a model like:




public function getUrlwithassetpath()

{

	// First publish the asset

	$asset = Asset::register($this);

	// get the baseUrl

	$pad = $asset->baseUrl;

	// strip all (incorrect) paths in the field url

	$urlwithoutextension = pathinfo($this->url)['filename'];

	// give the correct path back

	return $pad."/submap/".$urlwithoutextension;

}



However, the line Asset::register($this) gives an error. I am in a model, not a view, that is why.

But I need the path in this virtual attribute. How can I get the Asset path in a model?

P.S. the topic line is incorrect. This is in the model, not the controller. I can not edit the topic line?




$asset = Asset::register(\Yii::$app->view);



But if you really need this in the model, you are probably trying to use asset bundles for something they aren’t designed for.

I am in a module. So I have to use an asset path.

I my view I have a d3.js function callback like:

d3.json("/somecontroller/someaction", function(error,j) {

getting a json file from Yii2.

The controller has to know where the assets are to return a correct json.

How do I solve this problem the Yii2 way?

i ended up loading the javascript in a view and find the asset->baseUrl, adding this parameter to the ds.json call back (path=asset->baseUrl) and processing the parameter in the controller.