├── .gitignore ├── src ├── config │ └── tojs.php ├── ToJsFacade.php ├── ToJsController.php └── ToJsServiceProvider.php ├── composer.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | -------------------------------------------------------------------------------- /src/config/tojs.php: -------------------------------------------------------------------------------- 1 | env('TOJS_VAR_NAME', 'laravel'), 5 | ]; 6 | -------------------------------------------------------------------------------- /src/ToJsFacade.php: -------------------------------------------------------------------------------- 1 | $value) { 14 | $this->data[$key] = value($value); 15 | } 16 | 17 | return $this; 18 | } 19 | 20 | public function get($key = null, $default = null) 21 | { 22 | if (!$key) { 23 | return $this->data; 24 | } 25 | 26 | return Arr::get($this->data, $key, $default); 27 | } 28 | 29 | public function forget($keys) 30 | { 31 | Arr::forget($this->data, $keys); 32 | 33 | return $this; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/ToJsServiceProvider.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom(__DIR__.'/config/tojs.php', 'tojs'); 18 | 19 | $this->app->singleton('ToJs', function () { 20 | return new ToJsController(); 21 | }); 22 | 23 | Blade::directive('tojs', function () { 24 | $template = "window." . config('tojs.var_name') . " = get()); ?>;"; 25 | 26 | return ""; 27 | }); 28 | } 29 | 30 | /** 31 | * Register the application services. 32 | * 33 | * @return void 34 | */ 35 | public function register() 36 | { 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yazvonov/laravel-tojs", 3 | "description": "Transform PHP Vars to JavaScript for Laravel", 4 | "keywords": ["laravel", "javascript", "js", "tojs", "php"], 5 | "homepage": "https://github.com/yazvonov/laravel-tojs", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Yaroslav Zvonov", 10 | "email": "zvonovyo@gmail.com" 11 | } 12 | ], 13 | "require": { 14 | "php": "^7.0|^8.0" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "Yazvonov\\LaravelToJs\\": "src" 19 | } 20 | }, 21 | "extra": { 22 | "laravel": { 23 | "providers": [ 24 | "Yazvonov\\LaravelToJs\\ToJsServiceProvider" 25 | ], 26 | "aliases": { 27 | "ToJs": "Yazvonov\\LaravelToJs\\ToJsFacade" 28 | } 29 | } 30 | }, 31 | "config": { 32 | "sort-packages": true 33 | }, 34 | "minimum-stability": "dev", 35 | "prefer-stable": true 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 yazvonov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Transform PHP Vars to JavaScript for Laravel 2 | 3 | ## How to install 4 | 5 | ``` 6 | composer require yazvonov/laravel-tojs 7 | ``` 8 | 9 | ## What It Does 10 | This package Transform PHP Vars to JavaScript. 11 | 12 | 13 | Put variables 14 | ```php 15 | ToJs::put([ 16 | 'variable1' => 'I want to see you in JavaScript', 17 | 'variable2' => 'I want to see you in JavaScript too', 18 | ]); 19 | ``` 20 | 21 | Add tag @tojs to you blade template before \ 22 | ```blade 23 | @tojs 24 | ``` 25 | 26 | You will see this code on you page, and you can you it in JavaScript like window.laravel.variable1, window.laravel.variable2 27 | ```javascript 28 | 29 | ``` 30 | 31 | ## Configuration 32 | You can change name in javascript, just add 33 | ``` 34 | TOJS_VAR_NAME=another_name 35 | ``` 36 | to your .env file. 37 | 38 | ### Security 39 | 40 | If you discover any security-related issues, please email [zvonovyo@gmail.com](mailto:zvonovyo@gmail.com) instead of using the issue tracker. 41 | 42 | ## License 43 | 44 | The MIT License (MIT). Please see [License File](LICENSE) for more information. 45 | --------------------------------------------------------------------------------