bor-attila / cakephp-ptj
CakePHP 插件,帮助将 PHP 变量传递到 JavaScript
1.2
2023-09-11 16:07 UTC
Requires
- php: >=8.1
- ext-json: *
- cakephp/cakephp: >=5.0
Requires (Dev)
- phpunit/phpunit: ^8.0
README
轻松传递 PHP 变量到 JavaScript
此插件需要:CakePHP >=4.x
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
安装 composer 包的推荐方式是
composer require bor-attila/cakephp-ptj
在您的 Application.php 中启用插件
$this->addPlugin('PhpToJavascript');
在 View\AppView.php 中,将以下内容添加到 initialize 方法中
$this->loadHelper('PhpToJavascript.PhpToJavascript');
TL;DR
而不是
echo $this->Html->scriptBlock('var user = ' . json_encode(['firtname' => 'John', 'lastname' => 'Connor', 'age' => 15]));
直接使用(在布局中)
<html>
<head>
<?php
$this->PhpToJavascript->add([
'user' => ['firtname' => 'John', 'lastname' => 'Connor', 'age' => 15],
'language' => 'hu_HU'
]);
// or ..
$this->PhpToJavascript->add('key', 'value');
?>
</head>
<body>
...
<?= $this->PhpToJavascript->get(); ?> <!-- Place before all of your script tags -->
<?= $this->Html->script(['libraries.min', '....']); ?>
<?= $this->fetch('script') ?>
</body>
</html>
在客户端,您可以这样使用
p('user'); // Object { firtname: "John", lastname: "Connor", age: 15 }
何时以及为什么?
当您构建一个混合的 CakePHP 和 JavaScript 应用程序时,有时您希望将一些变量传递到客户端。当然,您可以为此构建一个 API - 但由于请求已经“到达控制器”(以获取模板),您可以简单地“echo”变量,如果您想以更优雅的方式完成此操作,则可以使用此插件。
我认为这比构建 API 更好,例如,只是为了知道:用户的 IP 地址、当前用户 ID、当前语言等 ...
配置
默认配置是
'function' => 'p',
'storage' => '__phptojavascript',
'encode' => [
'options' => 0,
'depth' => 512,
],
'cache' => [
'enabled' => true,
'key' => '__phptojavascript',
'config' => 'default',
]
function
帮助访问存储变量的函数名称。storage
存储 PHP 变量的全局 JavaScript 变量名称。请确保这是一个唯一的变量名称。encode
json_encode 函数的参数cache
基于function
和storage
配置始终生成的全局函数。您可以使用此参数来存储它。
使用(PHP)
//add multiple variable at once
$this->PhpToJavascript->add([
'user' => $user,
'language' => $lang
]);
//adding simple number if does not exists `var1`
$this->PhpToJavascript->add('var1', 1);
//adding array if does not exists `user`, note that you don't need to call json_encode. The plugin will do it for you
$this->PhpToJavascript->add('user', ['firtname' => 'John', 'lastname' => 'Connor', 'age' => 15]);
//setting and array if exists, will be overwritten
$this->PhpToJavascript->set('user', ['firtname' => 'John', 'lastname' => 'Connor', 'age' => 15]);
//or remove before $this->PhpToJavascript->get() is called
$this->PhpToJavascript->remove('user');
//prints the main javascript file containing the window.__phptojavascript variable and the p function
echo $this->PhpToJavascript->get();
使用(JavaScript)
//the default p function
p(string key, function mutator, default);
p('not_exists'); // undefined
p('user', null, false); // { .... }
p('not_exists', null, false); // false
p('user'); // Object { firtname: "John", lastname: "Connor", age: 15 }
p('user', user => user.age); // 15
// from version 1.1, the second parameter can be a default value or a mutator
// p(string key, any default);
// if the requested key WAS NOT found
//
// 1. then the second parameter is returned if there is only 2 parameter given
console.assert(p('not_exists', false) === false);
// 2. then the second parameter is ignored, and the third parameter is returned if there is 3 parameter or more
console.assert(p('not_exists', 'ignored', false) === false);
// 3. then the default result is undefined
console.assert(typeof p('not_exists') === 'undefined');
// if the requested key WAS found
//
// 1. then the value stored on the PHP side will be returned
console.assert(p('user') === window.__phptojavascript.user);
// 2. and the second parameter is a function, then the result will be 'mutated' by the function
console.assert(p('user', x => x.age) === 15);
// 3. and the second parameter is not a function, then the value stored on the PHP side will be returned and the other
// parameters are ignored
console.assert(p('user', 'not a function, so this is ignored', 'default value, but ignored') === window.__phptojavascript.user);
打包主要函数或扩展
您可以使用 generate_php_js 命令生成一个具有 p
函数的静态文件。该命令生成一个具有 window.__phptojavascript = {}; p = ...
初始化的静态 js 文件。您可以选择扩展它或打包它,但不要忘记告诉 get 方法。
bin/cake generate_php_js
echo $this->PhpToJavascript->get(false);
这可以防止生成并将主函数包含到 js 输出中。
待办事项
测试。