简体   繁体   English

如何在 Symfony 中将 PHP 会话变量传递给外部 JS 文件

[英]How to Pass PHP Session Variable to external JS file in Symfony

Currently I am working in One PHP Symfony Project Regarding the Performance Evaluation of an Employee.目前我在一个关于员工绩效评估的 PHP Symfony 项目中工作。 So I need to know how we can get PHP session variable of an employee (Employee Role) Please see the php file ( $role = $_SESSION['empRole']; ) to an external js file.(js file also attached).所以我需要知道如何获取员工(员工$role = $_SESSION['empRole']; )的 PHP 会话变量I am using PHP Symfony framework.我正在使用 PHP Symfony 框架。

Actually I need to check who is logged in to the website.if it is admin/core members / Hr.实际上我需要检查谁登录了网站。如果是管理员/核心成员/人力资源。 I need to apply validation to some fileds only when the Coremembers login the website.仅当核心成员登录网站时,我才需要对某些文件应用验证。

Here is the code section that I need to pass $role to External JS file(in pastebin)这是我需要将 $role 传递给外部 JS 文件(在 pastebin 中)的代码部分

    public function executeIndex(sfWebRequest $request) {
        if (isset($_SESSION['empId'])) {
            $role = $_SESSION['empRole'];
            if ($role == "admin") {
                $empId = $this->getUser()->getAttribute('empId');
                $team_members = GroupwareEmployeesPeer::getAllSubordinatesId($empId`enter code here`);
                $nc = new Criteria();
                $nc->add(PeD`enter code here`ates`enter code here`Peer::NAME, 'Dashboard');
                $resultset = PeDatesPeer::doSelect($nc);

So Can you guys give me a solution regarding this, since I am stuck with this for a long time.那么你们能给我一个解决方案吗,因为我已经坚持了很长时间。

Please see thepastebin code for php and external js file.请查看 php 和外部 js 文件的 pastebin 代码。

https://pastebin.com/kbkLjSFa - PHP File. https://pastebin.com/kbkLjSFa - PHP 文件。

https://pastebin.com/M60Rg64U - JS file https://pastebin.com/M60Rg64U - JS 文件

Yes you can get the php variable into the js file .是的,您可以将php 变量放入js 文件中。

Method I:方法一:

Rename your.js file to.js.php and pass the php variable as a query string to this file where you link this file in your page.将 your.js 文件重命名为 .js.php 并将 php 变量作为查询字符串传递给您在页面中链接此文件的位置。

So, if you have some test.js file, rename it to test.js.php and use it in your page like this所以,如果你有一些 test.js 文件,将它重命名为 test.js.php 并像这样在你的页面中使用它

<script src="test.js.php?session_var=<?= $your_var_here;?>&any_var=<?= $any_php_var;?>"></script>

And in your js file, retrieve the values from query string parameters So inside test.js.php, you can simply在你的 js 文件中,从查询字符串参数中检索值 所以在 test.js.php 中,你可以简单地

var some_var = "<?= $_GET['session_var']";
var any_var = "<?= $_GET['any_var'];?>";
//rest of your logic goes here

Method II:方法二:

Use an AJAX request.使用 AJAX 请求。

var some_var, another_var; 
$.ajax({
  url : '/url-which-returns-session-vars', //return the session data in json from this url
  asnyc : false,
  dataType : 'json', 
  success : function(res){
     some_var = res.some_var;
     another_var = res.another_var;
  }
});
//rest of the logic goes here.

Both of these methods work.这两种方法都有效。 But I prefer the first method, since I don't have to write extra code.但我更喜欢第一种方法,因为我不必编写额外的代码。

Edit: Suppose the response of the ajax call looks like this编辑:假设 ajax 调用的响应如下所示

{
   some_var : 'value here',
   another_var : 'another value here'
}

So res in the success function argument contains this response and since the response is a JSON you can access these values using the .因此,成功函数参数中的res包含此响应,并且由于响应是 JSON,您可以使用. notation .符号 Thus,因此,

some_var = res.some_var;
another_var = res.another_var;

and since these variables are global variables, you can use them anywhere in your code in this file.由于这些变量是全局变量,因此您可以在该文件的代码中的任何位置使用它们。

 ?> <script> var php_variable = '<?php $variable ?>'; </script> <?php

Global variable should be declared before external js file include . Global变量应该在external js文件include之前声明。 in js file access it using this variable name php_variable在 js 文件中使用这个变量名php_variable访问它

After the above code.在上面的代码之后。 jquery external file should be included here jquery 外部文件应该包含在这里

<script src="external.js" ></script>

external.js外部.js

consol.log(php_variable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM