Blackjack
Blackjack

Reputation: 1065

PHP: How do I can access .js and .csss in Web dir from my Module in yii2?

I've an application build in Yii2 framework, and I've develop the application in backend side, but know I want to copy them become a module.

This my application dir

backend
--modules
  --pstk
    --controllers
      --ValidationController.php
      --DefaultController.php
      --InterviewController.php
      --StudentController.php
      --SiteController.php
      --UsersController.php
    --models
      --Validation.php
      --Interview.php
      --Student.php
      --Users.php
    --views
      --validation
        --_form.php
        --view.php
        --index.php
        --update.php
        --create.php
      --interview
      --student
      --users
    --Module.php
--web
  --css
    --manual_general.css
  --js
    --upload_header.js
  --style

For example I've successfully run view.php from the module in browser, but the module can't access the .css and .js from backend/web/. So the view displayed, but it's view mashed-up and all button didn't do any action when clicked.

Anyone know why the module didn't access the manual_general.css and upload_header.js from backend/web/? and how I can solve this problem?

Any help, will be appreciated. Thanks :)

EDITED:

This is the code in mi view.php to connect to .js and .css

<link rel="stylesheet" type="text/css" href="../../../../web/css/manual_general.css">
<script type="text/javascript" src="../../../../web/js/upload_header.js"></script>

Upvotes: 0

Views: 68

Answers (1)

sonofagun
sonofagun

Reputation: 535

Try using yii\helpers\Url::to, like:

<link rel="stylesheet" type="text/css" href="<?= Url::to('@web/css/manual_general.css') ?>">
<script type="text/javascript" src="<?= Url::to('@web/js/upload_header.js') ?>"></script>

Alternatively, in view.php you may do (assuming $this is your View instance):

<?php $this->registerCssFile('@web/css/manual_general.css'); ?>
<?php $this->registerJsFile('@web/js/upload_header.js'); ?>

Upvotes: 1

Related Questions