More than once i have found my self having to host my backend and my frontend separately, and running multiple deployments. Although it has its benefit, it can be quite expensive to maintain. Deploying your react and laravel app on the same server still gives you most of the benefits of deploying them separately. They might live on the same server but they are still two reusable entities communicating with each other. Let me show how you can set up your laravel and react project on one server.
First, we are going to create our laravel project. We can do that by running the command.
laravel new react-laravel
You can learn more about creating a laravel project here.
/resources/view/ folder, we can find a file named
welcome.blade.php. Delete the file and create a new file called
index.blade.php with the following content.
Line 16 checks if we are running in development mode so that it can fetch the compiled asset from the webpack dev server. It's important that you update your
APP_ENV in the env file to 'production' when in a production environment.
Then we have to modify our route to point to that file. So will head into our routes folder and open
web.php. We will replace 'welcome' with 'index' and our file should end up looking like this:
Now it's time to modify our
package.json file which can be found in the root of the project. A number of modules will not be needed, so we should modify the file to look like this.
We will install all we need along the line.
Now we want to add react to our laravel project. Create a folder named 'src' in the root of our laravel project. Next, let's get Babel
To install babel, let's run
npm install --save-dev @email@example.com @firstname.lastname@example.org @email@example.com @firstname.lastname@example.org
We won't go into details of what each of this packages do so that this article doesn't get too long, but I will advise that you do your little research if you are not yet familiar with them.
Create a file named
.babelrc in the project root. We will be setting the presets for babel by inputting this content in the file.
Now we need to get and configure webpack. To do that we will need to install a few more packages. Let's run
npm install --save-dev email@example.com firstname.lastname@example.org email@example.com firstname.lastname@example.org email@example.com firstname.lastname@example.org
Webpack uses loaders to process different types of files. it also comes with a development server that will use to bundle and serve our React project during development. You can do some research on webpack if you are not already familiar with it.
Create a new file named
webpack.config.js also at the root of the laravel project. This file exports an object which will be our webpack configuration.
This configuration tells webpack what file to start bundling from (entry), the type of files that are transformed(module), files to leave out(exclude), and where it saves the bundled file to (output).
Let's go ahead and install a loader for processing sass files since it’s part of the modules we’ve defined in our config.
npm install sass sass-loader
You can add more loaders depending on your needs.
Next, we'll add two more packages by running
npm install --save email@example.com firstname.lastname@example.org
. Notice we are installing these as regular dependencies.
We can now create our
index.js file in the
src directory. This is the file that tells react when to hook into the dom.
We can also create another file in
App.js. All these should be familiar if you've previously worked with react.
From our webpack config, our app can also process css, so will create and add a css file. Create a folder named
style in our
src directory and in the
style directory create a filed named
App.css. We will add some style to this file
Your final project struture should be similar to this:
. +-- app +-- bootstrap +-- config +-- database +-- node_modules +-- public | +-- .htacess | +-- favicon.ico | +-- index.html | +-- index.php +-- resources | +-- views | | +-- index.blade.php +-- routes +-- src | +-- style | | +-- App.css | +-- App.js | +-- index.js +-- storage +-- test +-- vendor +-- .env +-- .env.example +-- .babelrc +-- .gitignore +-- artisan +-- composer.json +-- composer.lock +-- package-lock.json +-- package.json +-- server.php +-- webpack.config.js
We now have a function React & Laravel app! We can start up our dev server to compile our assets by running
and the start our php server to run the laravel app by running
php artisan serve
in the terminal. We should see our app on
If you run the server now, you will notice none of our changes on react updates the app. That is because HMR doesn't know what to replace yet.
We are going to complete our setup by installing
react-hot-loader. So run
npm install --save react-hot-loader
Now import react-hot-loader in our app and wrap it around the exported component. Your App.js should now look like this
Now our app will be updated as we make changes to the react app. Changing our PHP files will not cause the app to update, just the js files in the
When your app is ready for deployment, run
npm run build
to build our asset and update the APP_ENV in our env file to 'production' so that laravel will fetch the built asset and not the compiled asset in the dev server.
You can go ahead an tweak the setup according to your need. If anything is still unclear or you want another reference, here is a repo with the implementation on Github.