Download the PHP package bor-attila/cakephp-ptj without Composer
On this page you can find all versions of the php package bor-attila/cakephp-ptj. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bor-attila/cakephp-ptj
More information about bor-attila/cakephp-ptj
Files in bor-attila/cakephp-ptj
Package cakephp-ptj
Short Description Plugin for CakePHP which helps to pass PHP variables to Javascript
License MIT
Homepage https://github.com/bor-attila/cakephp-ptj/
Informations about the package cakephp-ptj
CakePHP - Php to Javascript
Easily pass PHP variables to Javascript
This Plugin requires: CakePHP >=4.x
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
Enable the plugin in your Application.php:
In View\AppView.php add this to the initialize method:
TL;DR
Instead of
just use (in layout)
In the client side you can use:
When and why ?
When you build a hybrid CakePHP and Javascript application, sometimes you want to pass some variable to client. Sure, you can build an API for this - but since the request has already "reached the controller" (to get the template) you can simply "echoes" the variables and if you want to have a nice way to do this - you can use this plugin.
I think this is a better way than building an API, for example, just to know: user's IP address, the current user id, the current language, etc ...
Configuration
The default configuration is:
function
the name of the function which helps to reach your stored variable.storage
the name of the global javascript variable where your php variables are stored. Just make sure this is an unique variable name.encode
parameters of the __json_encode__ functioncache
the global function always generated by based onfunction
andstorage
configuration. You can use this parameter to store it.
Usage (PHP)
Usage (Javascript)
Bundle the main function or extending
You can use the generate_php_js command to generate a static file with the p
function. The
command to generates a static js file with the window.__phptojavascript = {}; p = ...
initialization.
You can extend it or bundle it - your choose, but don't forget to tell this to the get method.
This prevents generating and including the main function into the js output.
Todo
Tests.