D
D
ddddd tttt2018-01-30 19:13:04
Yii
ddddd tttt, 2018-01-30 19:13:04

Why is alias @web empty and not configurable?

//Yii::setAlias('web',  dirname(dirname(__DIR__)) . '/web');  Если сделать так то вываливается куча    //ошибок 404 (site.css,main.js).
$this->registerJs('@web/js/main-index.js',      // Uncaught SyntaxError: Invalid or unexpected token
        $this::POS_HEAD,'main-index');
?>
<h1>main/index</h1>
<p>
    <?= print_r(Yii::$aliases);  ?> // @web => пусто. 
</p>

The markup looks like this
<script type="text/javascript">@web/js/main-index.js</script>.

Solved the problem.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-01-31
@pashaa

You obviously mixed up 2 methods
and the
first one registers a piece of js code and expects to receive a string as the first parameter, the second one registers a file and expects a string with a path to the file, including replacing aliases. So the first outputs exactly what it should. Just change the method.

A
Alexey, 2018-01-30
@AlexMaxTM

In general, all alices are registered in the config, as I understand it, you are trying to set an alias in the view.
You already have @web defined, but you are trying to override it, but in a different way dirname(dirname(__DIR__)) . '/web'because Yii no longer finds the css and js directories, hence a bunch of 404 errors.
Now next. Read carefully the $this->registerJs format, where the first parameter is the JS code. You have the line '@web/js/main-index.js', which is why you get complete nonsense like
Connecting external js scripts is done in the assets/AppAsset.php file
In the view, you can explicitly connect assets using AppAsset::register($this)
You can create your own connection classes and register them in views.
To get the value @web, call
echo Yii::getAlias('@web');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question