get backend var from php into reactjs via webpack

2019-09-14 05:43发布

How can I get a php backend var into my reactjs (using react-router) via webpack.

I want to do something like:

<?php 
  if ($us->inGroup('Admin')) { ?>
    <script>
      var groups = true
    </script>
<?php 
  }
?>

Then use that var groups inside my reactjs app via webpack (inside the bundle.js)

Here, where my reactjs app is -> <script type="application/javascript" src="/bundle.js"></script>

Thank you.

1条回答
戒情不戒烟
2楼-- · 2019-09-14 06:21

You don't need Webpack to do this. You just need to make sure that the groups variable is in the global scope. In your .php file, set the groups value by using a ternary statement to echo true or false based on the value return from the inGroup call.

<script>
  var groups = <?php echo $us->inGroup('Admin') ? 'true' : 'false'?>;
</script>
查看更多
登录 后发表回答