tutida / pack
该包的最新版本(2.0.2)没有提供许可信息。
CakePHP4变量在JS中
2.0.2
2020-08-05 06:54 UTC
Requires
- php: >=7.0.0
- cakephp/cakephp: ~4.0
README
您可以在视图中轻松地将CakePHP4变量传递到JS。
要求
- PHP >= 7.0
- CakePHP >= 4.0
安装
在Application.php中
<?php $this->addPlugin('Pack');
在控制器中。
<?php class AppController extends Controller { public function initialize() { $this->loadComponent('Pack.Pack'); } ... }
在布局php或模板php中。
<?= $this->Pack->render();?>
用法
只需在控制器中设置变量。
<?php
$entity = $this->Hoge->get($id);
$array = [...];
$this->Pack->set('entity', $entity);
$this->Pack->set('array', $array);
## OR ##
$this->Pack->set(compact('entity', 'array'));
只需在视图中获取JS中的变量。
Pack.entity; Pack.array;
方法
- set($varName, $variable) … 在Pack中设置变量。
- remove($varNamee) … 从Pack中删除变量。
- show() … 显示Pack中的所有变量。
- rename($namespace) … 在JS中更改Pack的命名空间。
示例
在控制器中
$this->Pack->rename('Hoge'); $this->Pack->set('array', $array);
在js中
Hoge.array;