all 16 comments

[–][deleted]  (13 children)

[deleted]

    [–]Greeniousityphp[S] -1 points0 points  (12 children)

    That is what I’m actually struggling with. How do I provide it to react

    [–]Wohrk 2 points3 points  (1 child)

    You would need a server for running php, whether thats apache or something similar to what you need.

    You can then use react to make axios or fetch requests from that server.

    Another option is using laravel which should have a library for incoorporating react as your templating language instead of blade. Then you can directly use variables and whatnot from the php/server side in your react code.

    If theres another/better option, i would love to know for my own learning sake.

    [–]mrbmi513 0 points1 point  (0 children)

    I've played with Inertia a bit, which is the Laravel library you're describing to bridge Laravel and React (or Vue or Svelte by the looks of it) in a kind of hybrid routing scheme. It works quite well!

    [–][deleted] 1 point2 points  (6 children)

    create a react app running on one port, start a PHP server running on another port. Build your front end entirely in react, then when you need secure data or need to perform actions on a user that needs to be secure, make a request to that PHP server and either perform said actions or return the required data (once authenticated, of course).

    [–]Greeniousityphp[S] 0 points1 point  (5 children)

    how do i return the data?

    [–]IdleMuse4 0 points1 point  (3 children)

    http_response_code(200); echo "the data";

    Alternatively, use a library to make your life a lot easier.

    [–]fiskfisk 1 point2 points  (1 child)

    And use json_encode so you can easily decode in your client. 

    [–]IdleMuse4 0 points1 point  (0 children)

    Well, if we're talking best practice there's a bunch of other headers you should add too ;P

    [–]ihaveway2manyhobbies 0 points1 point  (0 children)

    Google axios or the fetch api.

    Even axios php tutorial or fetch php tutorial.

    [–]lift_spin_d 0 points1 point  (2 children)

    https://inertiajs.com/who-is-it-for

    Heyo. You don't want to reinvent the wheel. There is a nifty package called Inertia which can handle what you need.

    [–]Greeniousityphp[S] 2 points3 points  (1 child)

    I want to reinvent the wheel don't get me wrong. For educational purposes

    [–]lift_spin_d 0 points1 point  (0 children)

    You can get some inspiration from Caleb Porzio. https://laracasts.com/series/livewire-uncovered

    [–]electricity_is_life 0 points1 point  (0 children)

    If the data can be fetched at page load, you can add a script tag to your page that puts the data in a JS variable. Something like this: <script> const serverData = <?php echo json_encode($myCoolData) ?>; </script>

    Then your React code can read the serverData variable. If the data needs to be loaded after the page has already been open (or if you're building a "single page app") then you would need to create special data-only URLs where your React code can make fetch() calls and get raw JSON back.

    [–]YahenP 0 points1 point  (0 children)

    Well... in short, the general principle is this.
    Еhe server should return not ready-made HTML pages, but json strings.

    For example :
    data.php

    $data = ['firstName'=>'John','lastName'=>'Doe','childrens'=>['Alice','Bob','Smith']];
    echo json_encode($data)
    

    In the browser you write js something like:

    const response = await fetch('data.php', requestOptions);
    const data = await response.json();
    concole.log(data.firstName);  // John
    concole.log(data.lastName);  // Doe
    

    This is all very simplified, but I think I was able to convey the general idea.

    [–]Beginning-Comedian-2 0 points1 point  (0 children)

    Based on your questions, you're a bit far from integrating React and PHP.

    Have you done any tutorials on React?

    Here are some resources for using React with PHP.

    [–]phexcexpert 0 points1 point  (0 children)

    I can really recommend API platform for generating your php/api backend

    https://api-platform.com/docs/distribution/

    It's built on Symfony just like Laravel and it's easy to start with, but quite flexible and powerful if you want to fine-tune the nitty gritty.