{"id":13396135,"url":"https://github.com/laracasts/PHP-Vars-To-Js-Transformer","last_synced_at":"2025-03-13T22:31:43.988Z","repository":{"id":14471302,"uuid":"17183369","full_name":"laracasts/PHP-Vars-To-Js-Transformer","owner":"laracasts","description":"Transform PHP data to JavaScript.","archived":false,"fork":false,"pushed_at":"2024-05-30T00:02:00.000Z","size":79,"stargazers_count":2211,"open_issues_count":24,"forks_count":219,"subscribers_count":56,"default_branch":"master","last_synced_at":"2024-10-29T15:04:16.785Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/laracasts/utilities","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/laracasts.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-02-25T18:24:45.000Z","updated_at":"2024-10-29T14:30:41.000Z","dependencies_parsed_at":"2024-11-27T14:49:12.456Z","dependency_job_id":null,"html_url":"https://github.com/laracasts/PHP-Vars-To-Js-Transformer","commit_stats":{"total_commits":95,"total_committers":24,"mean_commits":"3.9583333333333335","dds":0.3789473684210526,"last_synced_commit":"1cc5d5b9670e5be392df38e0b648fa4842c5a1f8"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracasts%2FPHP-Vars-To-Js-Transformer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracasts%2FPHP-Vars-To-Js-Transformer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracasts%2FPHP-Vars-To-Js-Transformer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracasts%2FPHP-Vars-To-Js-Transformer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laracasts","download_url":"https://codeload.github.com/laracasts/PHP-Vars-To-Js-Transformer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238188815,"owners_count":19430930,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T18:00:41.102Z","updated_at":"2025-03-13T22:31:43.966Z","avatar_url":"https://github.com/laracasts.png","language":"PHP","funding_links":[],"categories":["Popular Packages","Laravel","PHP","其他( Miscellaneous )"],"sub_categories":[],"readme":"# Transform PHP Vars to JavaScript\n\n[![Build Status](https://travis-ci.org/laracasts/PHP-Vars-To-Js-Transformer.png?branch=master)](https://travis-ci.org/laracasts/PHP-Vars-To-Js-Transformer)\n\nOften, you'll find yourself in situations, where you want to pass some server-side string/array/collection/whatever\nto your JavaScript. Traditionally, this can be a bit of a pain - especially as your app grows.\n\nThis package simplifies the process drastically.\n\n## Installation\n\nBegin by installing this package through Composer.\n\n```bash\ncomposer require laracasts/utilities\n```\n\n\u003e If you use Laravel 4: instead install `~1.0` of this package (and use the documentation for that release). For Laravel 5 (or non-Laravel), `~2.0` will do the trick!\n\n### Laravel Users\n\nFor Laravel users, there is a service provider you can make use of to automatically register the necessary bindings.\n\n\u003e Laravel 5.5+ users: this step may be skipped, as we can auto-register the package with the framework.\n\n```php\n\n// config/app.php\n\n'providers' =\u003e [\n    '...',\n    'Laracasts\\Utilities\\JavaScript\\JavaScriptServiceProvider'\n];\n```\n\n\nWhen this provider is booted, you'll gain access to a helpful `JavaScript` facade, which you may use in your controllers.\n\n```php\npublic function index()\n{\n    JavaScript::put([\n        'foo' =\u003e 'bar',\n        'user' =\u003e User::first(),\n        'age' =\u003e 29\n    ]);\n\n    return View::make('hello');\n}\n```\n\n\u003e In Laravel 5, of course add `use JavaScript;` to the top of your controller.\n\nUsing the code above, you'll now be able to access `foo`, `user`, and `age` from your JavaScript.\n\n```js\nconsole.log(foo); // bar\nconsole.log(user); // User Obj\nconsole.log(age); // 29\n```\n\nThis package, by default, binds your JavaScript variables to a \"footer\" view, which you will include. For example:\n\n```\n\u003cbody\u003e\n    \u003ch1\u003eMy Page\u003c/h1\u003e\n\n    @include ('footer') // \u003c-- Variables prepended to this view\n\u003c/body\u003e\n```\n\nNaturally, you can change this default to a different view. See below.\n\n### Defaults\n\nIf using Laravel, there are only two configuration options that you'll need to worry about. First, publish the default configuration.\n\n```bash\nphp artisan vendor:publish\n\n// Or...\n\nphp artisan vendor:publish --provider=\"Laracasts\\Utilities\\JavaScript\\JavaScriptServiceProvider\"\n```\n\nThis will add a new configuration file to: `config/javascript.php`.\n\n```php\n\u003c?php\n\nreturn [\n\n    /*\n    |--------------------------------------------------------------------------\n    | View to Bind JavaScript Vars To\n    |--------------------------------------------------------------------------\n    |\n    | Set this value to the name of the view (or partial) that\n    | you want to prepend all JavaScript variables to.\n    |\n    */\n    'bind_js_vars_to_this_view' =\u003e 'footer',\n\n    /*\n    |--------------------------------------------------------------------------\n    | JavaScript Namespace\n    |--------------------------------------------------------------------------\n    |\n    | By default, we'll add variables to the global window object. However,\n    | it's recommended that you change this to some namespace - anything.\n    | That way, you can access vars, like \"SomeNamespace.someVariable.\"\n    |\n    */\n    'js_namespace' =\u003e 'window'\n\n];\n```\n\n#### bind_js_vars_to_this_view\n\nYou need to update this file to specify which view you want your new JavaScript variables to be prepended to. Typically, your footer is a good place for this.\n\nIf you include something like a `layouts/partials/footer` partial, where you store your footer and script references, then make the `bind_js_vars_to_this_view` key equal to that path. Behind the scenes, the Laravel implementation of this package will listen for when that view is composed, and essentially paste the JS variables within it.\n\n#### js_namespace\n\nBy default, all JavaScript vars will be nested under the global `window` object. You'll likely want to change this. Update the\n`js_namespace` key with the name of your desired JavaScript namespace. It can be anything. Just remember: if you change this setting (which you should),\nthen you'll access all JavaScript variables, like so:\n\n```js\nMyNewNamespace.varName\n```\n\n#### Note\nRun this artisan command after changing the view path.\n```\nphp artisan config:clear\n```\n\n### Symfony2\nTo use this component in Symfony2 applications you can try [this bundle](https://github.com/holyspecter/HospectPhpVarsToJsBundle), built on top of PHP-Vars-To-Js-Transformer.\n\n### Without Laravel\n\nIf you're not using Laravel, then you'll need to hard-wire things yourself. (Or, feel free to submit a pull request with an implementation for your desired framework.)\n\nFirst, create an implementation of the `Laracasts\\Utilities\\JavaScript\\ViewBinder` interface. This class is in charge of inserting the given JavaScript into your view/page.\n\n```php\n\u003c?php\n\nclass MyAppViewBinder implements Laracasts\\Utilities\\JavaScript\\ViewBinder {\n\n    // $js will contain your JS-formatted variable initializations\n    public function bind($js)\n    {\n        // Do what you need to do to add this JavaScript to\n        // the appropriate place in your app.\n    }\n}\n```\n\nNext, put it all together:\n\n```php\n\nuse Laracasts\\Utilities\\JavaScript\\Transformers\\Transformer;\n\n$binder = new MyAppViewBinder;\n\n$javascript = new Transformer($binder, 'window'); // change window to your desired namespace\n\n$javascript-\u003eput(['foo' =\u003e 'bar']);\n```\n\nNow, you can access `window.foo` from your JavaScript.\n\nRemember, though, this is only necessary if you aren't using Laravel. If you are, then just reference the service provider, as demonstrated above.\n\n## License\n\n[View the license](https://github.com/laracasts/PHP-Vars-To-Js-Transformer/blob/master/LICENSE) for this repo.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaracasts%2FPHP-Vars-To-Js-Transformer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaracasts%2FPHP-Vars-To-Js-Transformer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaracasts%2FPHP-Vars-To-Js-Transformer/lists"}