When it comes to web development, my goto library on the front end is React, and on the backend, I love to use Laravel because of the many built-in goodies it contains.
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.
For this task, we are going to be making use of Babel, webpack and react-hot-loader to compile our react assets into the laravel view.
Laravel
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.
In our /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:
package.json
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.
React
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
Babel
To install babel, let's run ```npm
install --save-dev @babel/core@7.1.0 @babel/cli@7.1.0 @babel/preset-env@7.1.0 @babel/preset-react@7.0.0
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.
Webpack
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 webpack@4.19.1 webpack-cli@3.1.1 webpack-dev-server@3.1.8 style-loader@0.23.0 css-loader@1.0.0 babel-loader@8.0.2
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.
<div class="ltag_gist-liquid-tag">
<script id="gist-ltag" src="https://gist.github.com/codenaz/ae323388ecbb703cbaa64553bc3fac18.js"></script>
</div>
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.
</code></pre></div>
<p><br>
npm install sass sass-loader<br>
</p>
<div class="highlight"><pre class="highlight plaintext"><code>
You can add more loaders depending on your needs.
### React
Next, we'll add two more packages by running ```npm
install --save react@16.18.4 react-dom@16.8.4
```. 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.
<div class="ltag_gist-liquid-tag">
<script id="gist-ltag" src="https://gist.github.com/codenaz/2a3f397a6472916279f406bb9a3ca172.js"></script>
</div>
We can also create another file in `src` called `App.js`. All these should be familiar if you've previously worked with react.
<div class="ltag_gist-liquid-tag">
<script id="gist-ltag" src="https://gist.github.com/codenaz/76c9256fd950235c064da2001f0b941d.js"></script>
</div>
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
<div class="ltag_gist-liquid-tag">
<script id="gist-ltag" src="https://gist.github.com/codenaz/95ebfb7bffdeadcfb1cb1d7e48a233bc.js"></script>
</div>
.
Your final project struture should be similar to this:
</code></pre></div>
<p></p>
<p>.<br>
+-- app<br>
+-- bootstrap<br>
+-- config<br>
+-- database<br>
+-- node_modules<br>
+-- public<br>
| +-- .htacess<br>
| +-- favicon.ico<br>
| +-- index.html<br>
| +-- index.php<br>
+-- resources<br>
| +-- views<br>
| | +-- index.blade.php<br>
+-- routes<br>
+-- src<br>
| +-- style<br>
| | +-- App.css<br>
| +-- App.js<br>
| +-- index.js<br>
+-- storage<br>
+-- test<br>
+-- vendor<br>
+-- .env<br>
+-- .env.example<br>
+-- .babelrc<br>
+-- .gitignore<br>
+-- artisan<br>
+-- composer.json<br>
+-- composer.lock<br>
+-- package-lock.json<br>
+-- package.json<br>
+-- server.php<br>
+-- webpack.config.js</p>
<p></p>
<div class="highlight"><pre class="highlight plaintext"><code>
We now have a function React & Laravel app! We can start up our dev server to compile our assets by running ```npm
start
``` 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 `http://localhost:8000`
## HMR
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
<div class="ltag_gist-liquid-tag">
<script id="gist-ltag" src="https://gist.github.com/codenaz/9ea516e93ea379709fbac47489199fd1.js"></script>
</div>
.
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 `src` folder.
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.
## Conclusion
You can go ahead an tweak the setup according to your need. If anything is still unclear or you want another reference, [here](https://github.com/codenaz/reavel) is a repo with the implementation on Github.
</code></pre></div>
Top comments (2)
Hi, thx for article
is it possible to add css modules for blade files?
I have the same architecture laravel + Preact(i use portals for some parts) ,but most of the application is it blade and generated css from scss files
Everything frontend is taken care of by react in this setup, including css.