laracasts / PHP-Vars-To-Js-Transformer

Transform PHP data to JavaScript.
https://packagist.org/packages/laracasts/utilities
MIT License
2.21k stars 219 forks source link

Not working for Laravel 5.3 #91

Closed mschink closed 7 years ago

mschink commented 7 years ago

I've followed the documentation very closely and it's still missing something, perhaps namespace related?

Here's my composer.json:

    "require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.3.*",
    "laracasts/utilities": "~2.0"
    },

Then in cmd:

composer update

results in vendor\laracasts\utilities\ as expected

Then I modify app.php:

    'providers' => [
        /*
         * Laravel Framework Service Providers...
         */
        ...
        `Laracasts\Utilities\JavaScript\JavaScriptServiceProvider`
    ],

Here's the part where I think there's definitely some issue in 5.3, because php artisan vendor:publish gives the following output:

Copied Directory [\vendor\laravel\framework\src\Illuminate\Notifications\resources\views] To [\resources\views\vendor\notifications]
Copied Directory [\vendor\laravel\framework\src\Illuminate\Pagination\resources\views] To [\resources\views\vendor\pagination]
Publishing complete for tag []!

No mention of the config for javascript, and there's no javascript.php file copied to config/

But maybe it will still work, so in my IndexController.php

namespace App\Http\Controllers;
...
use JavaScript;
...
class IndexController extends Controller
{
  public function getIndex(){
  JavaScript::put([
    'foo' => 'bar'
    ]);

Results in the following error:

FatalThrowableError in IndexController.php line 38:
Class 'JavaScript' not found
eugenefvdm commented 7 years ago

I tested with two Laravel 5.3 projects, one small site and a completely new install, and it worked.

Some pointers:

  1. In config/app.php I needed:

Laracasts\Utilities\JavaScript\JavaScriptServiceProvider::class,

as I have a later PHP version. Make absolutely sure you have this as above.

  1. I had to create a blank footer.blade.php. The documentation is a bit unclear, but if you don't have that file, create it (and blank worked).

  2. Your actual error of class not found sounds like something fundamental is wrong.

  3. I didn't need to publish to get it working.

mschink commented 7 years ago

Thanks for the detailed response.

I did try the ::class notation for later versions of PHP, but no luck with that. I currently have a file footer.blade.php in views/templates/ so I'm not sure if this requirement is directory-sensitive.

I will try doing this with a brand new project, and report the results. These things might be nice to include in the documentation so there's less clutter of questions regarding setting up the package!

Cheers.

UPDATE: For anyone else having similar issues as I had above, I highly suggest rebuilding your app from scratch. As far as I know, I did nothing different, but it's working now. Consider this issue resolved, though, an update to the documentation would be great for first-timers who haven't installed a 3rd party package via Composer.

jamesmorrish commented 7 years ago

I've just been tearing my hair out for the past hour. It just wasn't running the boot method on the service provider.

Eventually I got it to work by running

php artisan config:clear php artisan config:cache php artisan clear-compiled

Mutasim0520 commented 7 years ago

I am also having the same problem? @mschink did you solve it?

mschink commented 7 years ago

@Mutasim0520 I think if you follow what @jamesmorrish said above you'll find it works. It could be related to caching issues or perhaps my unfamiliarity with using Composer. Because I have my entire project versioned on Git, the last resort was simple enough: clone a new copy of the project and see if it works there.

TinoN commented 7 years ago

@eugenevdm to make your post a little clearer: I only got it working if the code is within the footer.blade.php file u mentioned.

EDIT: behaviour is accordingly, the configuration is bound to

    'bind_js_vars_to_this_view' => 'footer',

(README.md)

Laravel: 5.4 PHP-Vars-To-Js-Transformer: 2.1

  1. composer require laracasts/utilities
  2. in config/app.php add
    'providers' => [
    ...,
    Laracasts\Utilities\JavaScript\JavaScriptServiceProvider::class,
    ...
    ];
  3. in routes/web.php
    Route::get('/', function () {
    JavaScript::put(['foo' => 'bar']);
    return view('welcome');
    });
  4. for example in welcome.blade.php add before <\/body>
         @include ('footer')
    </body>
  5. create footer.blade.php and within this file add
    <script>console.log(foo);</script>
  6. php artisan serve
  7. check console output, should be:
    bar