Skip to content

Commit 3b0ac5b

Browse files
author
Danut Florian
committed
Added publishable views support
When using a CSP, we need to add nonce support to the generated script tag.
1 parent 1cc5d5b commit 3b0ac5b

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

readme.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ For Laravel users, there is a service provider you can make use of to automatica
3333
];
3434
```
3535

36-
3736
When this provider is booted, you'll gain access to a helpful `JavaScript` facade, which you may use in your controllers.
3837

3938
```php
@@ -83,7 +82,7 @@ php artisan vendor:publish
8382
php artisan vendor:publish --provider="Laracasts\Utilities\JavaScript\JavaScriptServiceProvider"
8483
```
8584

86-
This will add a new configuration file to: `config/javascript.php`.
85+
This will add a new configuration file to: `config/javascript.php`
8786

8887
```php
8988
<?php
@@ -116,6 +115,12 @@ return [
116115
];
117116
```
118117

118+
and copy the view to `views/vendor/utilities/javascript.blade.php`
119+
120+
```php
121+
<script>{{$js}}</script>
122+
```
123+
119124
#### bind_js_vars_to_this_view
120125

121126
You 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.
@@ -129,16 +134,19 @@ By default, all JavaScript vars will be nested under the global `window` object.
129134
then you'll access all JavaScript variables, like so:
130135

131136
```js
132-
MyNewNamespace.varName
137+
MyNewNamespace.varName;
133138
```
134139

135140
#### Note
141+
136142
Run this artisan command after changing the view path.
143+
137144
```
138145
php artisan config:clear
139146
```
140147

141148
### Symfony2
149+
142150
To 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.
143151

144152
### Without Laravel

src/JavaScriptServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ public function register()
3434
public function boot()
3535
{
3636
$this->publishes([
37-
__DIR__ . '/config/javascript.php' => config_path('javascript.php')
37+
__DIR__ . '/config/javascript.php' => config_path('javascript.php'),
38+
__DIR__ . '/views' => base_path('resources/views/vendor/utilities')
3839
]);
3940

41+
$this->loadViewsFrom(__DIR__ . '/views', 'utilities');
42+
4043
if (class_exists('Illuminate\Foundation\AliasLoader')) {
4144
AliasLoader::getInstance()->alias(
4245
'JavaScript',

src/LaravelViewBinder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Laracasts\Utilities\JavaScript;
44

55
use Illuminate\Contracts\Events\Dispatcher;
6+
use Illuminate\View\View;
67

78
class LaravelViewBinder implements ViewBinder
89
{
@@ -39,9 +40,11 @@ public function __construct(Dispatcher $event, $views)
3940
*/
4041
public function bind($js)
4142
{
43+
4244
foreach ($this->views as $view) {
4345
$this->event->listen("composing: {$view}", function () use ($js) {
44-
echo "<script>{$js}</script>";
46+
$utilities = view("utilities::javascript", ['js' => $js]);
47+
echo $utilities->render();
4548
});
4649
}
4750
}

src/views/javascript.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script>{!! $js !!}</script>

0 commit comments

Comments
 (0)