Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnniecoder's avatar

pass data from php to vue in non-spa set up ?

how do you guys pass data from php to vue in non-spa set up ?

how i do (it is working):

controller:

Route::get('server-to-vue',function(){
    //use laracasts/utilities package
    \JavaScript::put([
        'test'=>request()->input('test'),
        'countries' =>\App\Utilities\Countries::all(),
    ]);
    return view('blade');
});

component.vue

<template>
    <div>
        <select>
          <option v-for="(country,country_code) in countries" v-bind:value="country_code">
            {{ country }}
          </option>
        </select>            
        
        <hr>
        $_GET['test'] : {{test}}
    </div>
</template>

<script>
    import common_mixin from './CommonMixin'

    export default {
        mixins : [common_mixin],
        server_variables : ['test','countries'],
        data() {
            return {
                test:'',
                countries : [],
            }
        },
        mounted() {  
            this.loadDataFromGobals();
        },
        methods: {
        }
    }
</script>

CommonMixin.js

export default {
    methods: {
        loadDataFromGobals: function() {
            let variables = this.$options.server_variables
            
            if(variables == undefined)
                return ;

            let self = this;

            variables.map(variable => {
                self[variable] = window[variable];
            });
        }
    }
}

any suggestion to this implementation? any better implementation?

0 likes
13 replies
EventFellows's avatar

You are obviously using Jeff's package here: https://github.com/laracasts/PHP-Vars-To-Js-Transformer (as a reference for others)

Did you follow the instructions given on the github page about attaching the variables to the footer to a specific js_namespace?

Then should be able to just use the variables in your data() method like this:

        data() {
            return {
                test: your_js_namespace.test,
                countries: your_js_namespace.countries,
            }
        },
johnniecoder's avatar

yes i am using Jeff's package and every things is working.

I just want to start a discussion to find a better way/pattern to do the job

johnniecoder's avatar

other ways i tried but not like

#pass data by html attribute #call api in mounted

andonovn's avatar

I usually pass it as component property like this <component-name :property-name='<?= htmlspecialchars(json_encode($foo)) ?>'></component-name>

I don't like the additional AJAX request on mounted/created

Abi's avatar

You can also create a provider and send javascript :)


  public function boot()
    {
        \View::composer('*', function($view){


            echo "<script>window.hello = 'a';</script>";

        });
    }


johnniecoder's avatar

@andonovn ,component property dont work well for me too , 1 . imagine a search form that has 10 search criteria , you then need 10 properties to pass the data needed for displaying the search criteria user currently using 2. some data just aren't that easy to pass by property from html , eg in php you have an array like ["AF"=> "Afghanistan", "AL"=> "Albania" .....], and you want to pass it to vue to generate a selection box

andonovn's avatar

@johnniecoder Component properties are working absolutely fine for me.

In your scenarios:

  1. You can just pass an array $filters that is containing all the 10 search criterias.
  2. In the example I gave $foo can easily be an array and it will work fine.
johnniecoder's avatar

@andonovn , thank you for your suggestion, is there any way that i can tell vue to " auto json_decode Component properties " . if no , do i just "json_decode" in mounted() ?

willvincent's avatar

yes i am using Jeff's package and every things is working. I just want to start a discussion to find a better way/pattern to do the job

Honestly the only thing really better than that package is to simply serve up the empty page and vue components, then make an ajax request back to the server to fetch data.

1 like
Abi's avatar

@willvicent I actually like the provider approach that I mention earlier as you don't really need to pull in a new package to do it and it gives you full control over which pages can access the variable without having to repeat yourself or keep sending ajax request.

andonovn's avatar

@johnniecoder I believe Vue is magically decoding it to array/object when we use v-bind: before the property. In my example above I used the shorthand : which is the same as v-bind:

@willvincent why do you think using component properties is worse than sending additional requests to the server? I am asking because I would like to know if there is something wrong the component properties solution as I am using it in all the projects I work on.

willvincent's avatar

@Abi that's essentially all Jeff's package does anyway :)

@andonovn nothing wrong with it per se, just feels a little bit hacky to me for long-term use. Personal preference I guess.

Please or to participate in this conversation.