Building a BigCommerce App Using Laravel and React

Nate Stewart
Apr 25, 2019 · 10 min read

As Head of Product Strategy at BigCommerce, I often get to work with developers that are looking to quickly extend what our platform can do and enable a UI for merchants to interface with the new functionality they create. Something I’ve been wanting to offer those developers is a way to fast-track their concepts into a functional app on our platform — a boilerplate app using a popular BE and FE framework that is easily accessible.

While waiting for that problem to be magically solved, I had been prototyping a concept on my own time using some of our new Widget APIs, and realized out of necessity I had already created the foundation for a simple Laravel & React based app in the process.

This article documents those steps: what it took to get to a functional app using Laravel on the back-end and React on the front-end. Hopefully it fast-tracks your understanding of what it takes to quickly build your first BigCommerce app using approachable, modern frameworks. At the end, you’ll have a basic two-screen app (with BE and FE routing) you can run locally, which can be installed on a BigCommerce store and run API requests against it.

And yes, the link to GitHub with the final code is at the bottom.


Baseline Needs

Before jumping in, you’ll want to make sure you have installed the following dependencies on your dev machine:

To ease PHP development and enable the app you develop to be easily shared, you’ll want to use either Valet or Homestead, depending on your OS:

We’ll be using Valet for some of the steps below, but the functionality to host and share sites is similar across both Valet and Homestead. What’s more important in this tutorial is how to configure Laravel to use React and connect with BigCommerce.


Step 1: Getting Laravel and React Running Together

This is where we will create a baseline for future development: a simple application that loads at a specific URL in your browser and loads a React component instead of the default Laravel screen.

Set up a directory to serve the app

We need to first set up a directory to store this and any future app you develop this way. You may have already run this during the Valet set-up process — in that case jump to the next step.

Create a new Laravel codebase

Use the Laravel command that creates the initial boilerplate for an app in the ~/Sites directory:

You should see the command run its course, like this:

This command usually completes with a ‘Application ready! Build something amazing.’

Visit the app address to make sure it’s live locally

After the command above completes, you should be able to visit the following URL in your browser and see the default Laravel welcome screen:

http://laravel-react-bigcommerce-app.test

You should be looking at the Laravel welcome screen at this point.

Note: If you see It Works! instead, you’ll need to stop the Apache server running on localhost. See this Stack Overflow post for more details.

Make sure your app runs over HTTPS

As you can see by the ‘Not Secure’ that shows in the browser, the app by default will be served via HTTP. To serve via HTTPS instead, run the following command in the app directory:

Now https://laravel-react-bigcommerce-app.test should work. If it doesn’t and you receive a connection refusal in the browser, try editing the Site.php Valet file as described here and rerunning valet secure (as noted in this GitHub issue comment).

Your app should now be served over HTTPS.

Set up React as the JS framework

By default Laravel comes pre-configured with Vue.js. We want to use React, so we’ll switch to it using the following command in your app root dir:

Now you are set up with React scaffolding in Laravel. However, you’ll still be getting the same Laravel welcome screen. We’ll fix that next.

Set up index page template to load the React app

The default page for a Laravel app is the welcome screen that’s in the welcome.blade.php file. We want to initialize React instead.

To do this, make the following changes:

  1. Add an app.blade.php file into the /resources/views directory with the contents below.
  2. Update the main route in /routes/web.php point to ‘app’ instead of ‘welcome’.
  3. Delete welcome.blade.php

Note that we’re including jQuery and Moment.js as dependancies as well, since they are extremely common needs when building out an app. They are not required for React to function.

Check Node.js version and install JS dependancies

For the initial JS compile and future steps, I was using Node v10.14.1. While it’s not required and you should be able to use new versions, I recommend using NVM or a similar library to help you manage your different Node versions easily. You can learn more about NVM and alternate managers here.

Now, install any dependancies your JS app needs by running this command your root app directory:

After a minute or two, you should have all the dependancies loaded.

Here’s an example of NVM being used to pick a specific Node version before installing dependancies.

Compile JS assets

Now that the dependancies are installed, you are ready to compile the JS assets into something that loads in the browser. Run:

You’ll notice you get a readout of how long certain assets took to compile and a notification from Laravel Mix when it’s done building.

Note that Mix is effectively a convenience wrapper around webpack, which can be confusing at first to understand. If you don’t already know about it, put webpack on your bucket list of research items. It’s a good tool to know how to wield within more complex projects.

Ok, let’s view https://laravel-react-bigcommerce-app.test/ now:

Wait… nothing is loading!?

Actually. React is, as you can see from the React DevTools being suggested. However, we don’t have any React components displaying.

Render your first React component

The Laravel React preset actually contains an example component that is already included in the initialization. By default it is looking for a DOM id that isn’t in the app.blade.php template.

To fix that, change the ‘example’ id references in /resources/js/components/example.js to ‘root’, which does exist as a DOM id in the template.

So this, at the bottom of that component file:

Changes into this:

After that change. Run the compile assets command again:

And when it’s complete, refresh you browser to see the component!

Our example React component loaded in the browser.

Note that this React component and others in this article are all using Bootstrap for layout and styling.

Step 2: Set up Basic App Routes

Now you are set up for React development with a Laravel back-end. However for a functional ecommerce app you’ll want two more pieces in place: routing and access to external data via an API. We’ll focus on routing first.

We’ll be using React Router for this. To install the dependency, run:

Once that is added, you are ready to implement routes within the app. Using the code below as a reference for changes needed, alter the following files:

  1. /resources/js/app.js -> remove the example component require and instead require a new index.js file
  2. /resources/js/screens/home.js -> new file that will render the home screen
  3. /resources/js/index.js -> new file that will handle routing and render the nav
  4. /resources/js/screens/list.js -> new file that will render the list screen
  5. /routes/web.php -> update the back-end route that loads the main app to also load for the new ‘list’ route, which will enable the browser to load the right screen for https://laravel-react-bigcommerce-app.test/list regardless if it is navigated to directly (url) or indirectly (app link click)

After all those changes, run:

Reload your browser at https://laravel-react-bigcommerce-app.test and you’ll now see a functional layout for an app, including navigation, appear!

Looking more like an app, eh?

Step 3: Connect the App With BigCommerce

Alright, we have a good base now with React routing, so it’s time to start connecting the app to real data inside a BigCommerce store.

Create app in BC dev tools area

Head to devtools.bigcommerce.com and log in with your BigCommerce store account. Create an app and go to the ‘Technical’ step in them modal.

To start, you want at least the auth and load callback URLs to be set, since those are what BC will use to initiate the install process and enable the app to load within the BC control panel.

The callback URLs I used when developing.

After setting the callback URLs, you need to select which scopes your app will need. Keep track of this because your app will need to check the proper scopes have been granted when installed. If you were making a real app, you would only select what the app actually needs here, as BigCommerce will work to ensure you don’t have too many permissions.

The scopes I used when developing. Only select what you’ll actually use!

Save your app’s client ID and secret

The client ID and Secret are used to verify that your app requests are valid. Save these into environment variables within your app. In the sample Laravel app, we’re saving these in the .env file along with the scopes so each piece of app info is able to be set in one place.

Update your .env file (in the root app directory) to have the APP_URL set as https://laravel-react-bigcommerce-app.test and add new env variables at the bottom of the file for your BC App IDs and test API credentials for local dev.

Install Guzzle dependency

You can see above that there are calls to the BC API which are using Guzzle. To add that as a dependency, in the root of the development directory run:

Set up the install, load and BC API proxy routes

When the app is installed, it will look to the callbacks that are defined in the dev tools area.

Add the web routes below, so Laravel knows to route to the specific controller methods for each callback. We are implementing install and load here, to get the baseline experience working, however there are stubbed routes for future functionality like uninstall and remove-user too.

Create controller to handle app install and load requests, proxy BC API

You can see above that there are references to ‘MainController’. That is where we’ll put the logic that handles the OAuth handshake and stores the credentials generated for the store. Keep in mind this uses session based storage, so when the browser session expires, the app will stop working.

The last route is a proxy through to the BigCommerce v2 and v3 APIs. We’ll use that to enable a bc-api endpoint we can hit on the front-end, which helps us bypass CORs issues.

To power these routes, add the following MainController.php file into your /app/Http/Controllers directory:

Major Note: By default, your app is set to use your hardcoded API credentials in the .env file. When you install the app within BigCommerce, you want your app to use the credentials passed back during the OAuth token exchange. To do this, make sure your APP_ENV config value in your .env file is set to production, like so:

Now, if you head to your BC store admin, to the Apps -> My Apps -> My Draft Apps section, you can install your app and see it successfully load inside the control panel.

Now the app is installable within BigCommerce.

Create a front-end experience that surfaces data in BigCommerce

All the pieces are in place to create front-end components that actually do something, so I created a simple set of React components and screens that:

  • Load brief catalog summary and store information
  • List the last 10 orders and enable the user to cancel them

The scopes that are required were:

  • Orders: Modify
  • Products: Read-only
  • Information and Settings: Read-only

To enable the front-end components to hit the API using the back-end BigCommerce API Proxy endpoints in MainController.php, add the following files to a new /resources/js/services/ directory:

The actual components are nice and light. Which is the point, right? Three files handle this:

  1. /resources/js/components/index.js -> new file that handles importing multiple components (simplifies inclusion into screens)
  2. /resources/js/components/Table/index.js -> new file that contains a basic Table component
  3. /resources/js/components/Spinner/index.js -> new file that contains a basic Spinner component

/resources/js/components/index.js

/resources/js/components/Table/index.js

/resources/js/components/Spinner/index.js

Now, with the API service and components in place, the screens can be updated to produce something functional. To bring it all together, change the following files:

resources/js/screens/home.js

resources/js/screens/list.js

And as the final step, compile the JS assets again:

After a successful compile, you’ll now have a functional Laravel React app which can be loaded inside of BigCommerce!

Next Steps

If you got this far, congrats! You have a great base to work on as you experiment with all the BigCommerce APIs.

To launch a real app, aside from hosting it on a server other than your dev box, you’ll still need to add some persistent storage for API credentials, storing the store and user info received from the OAuth token request during app install so users can load the app after the initial session expires. Error handling, especially for failed requests to the API, should be handled and surfaced to the merchant. And tests, once you get to a state you are reasonably happy with, will help keep regressions at bay.

The Code

https://github.com/bigcommerce/laravel-react-sample-app/releases/tag/1.0

Now go build something amazing!

BigCommerce Developer Blog

News, tips, and stories for developing on BigCommerce

Nate Stewart

Written by

Head of Product Strategy @ BigCommerce

BigCommerce Developer Blog

News, tips, and stories for developing on BigCommerce

More From Medium

More from BigCommerce Developer Blog

More from BigCommerce Developer Blog

More from BigCommerce Developer Blog

Let’s Talk About CORS

More from BigCommerce Developer Blog

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade