Building a BigCommerce App Using Laravel and React

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 HTTPSAs 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:valet secureNow 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 frameworkBy default Laravel comes pre-configured with Vue.


We want to use React, so we’ll switch to it using the following command in your app root dir:php artisan preset reactNow 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 appThe default page for a Laravel app is the welcome screen that’s in the welcome.


php file.

We want to initialize React instead.

To do this, make the following changes:Add an app.


php file into the /resources/views directory with the contents below.

Update the main route in /routes/web.

php point to ‘app’ instead of ‘welcome’.

Delete welcome.


phpNote 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 dependanciesFor the initial JS compile and future steps, I was using Node v10.



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:npm installAfter 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 assetsNow that the dependancies are installed, you are ready to compile the JS assets into something that loads in the browser.

Run:npm run devYou’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 componentThe 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.


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:if (document.

getElementById('example')) { ReactDOM.

render(<Example />, document.

getElementById('example'));}Changes into this:if (document.

getElementById('root')) { ReactDOM.

render(<Example />, document.

getElementById('root'));}After that change.

Run the compile assets command again:npm run devAnd 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 RoutesNow 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:npm install –save react-router-dom 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:/resources/js/app.

js -> remove the example component require and instead require a new index.

js file/resources/js/screens/home.

js -> new file that will render the home screen/resources/js/index.

js -> new file that will handle routing and render the nav/resources/js/screens/list.

js -> new file that will render the list screen/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:npm run dev 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 BigCommerceAlright, 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 areaHead to devtools.


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 secretThe 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 dependencyYou 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:composer require guzzlehttp/guzzleSet up the install, load and BC API proxy routesWhen 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 APIYou 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:APP_ENV=productionNow, 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 BigCommerceAll 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 informationList the last 10 orders and enable the user to cancel themThe scopes that are required were:Orders: ModifyProducts: Read-onlyInformation and Settings: Read-onlyTo 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:/resources/js/components/index.

js -> new file that handles importing multiple components (simplifies inclusion into screens)/resources/js/components/Table/index.

js -> new file that contains a basic Table component/resources/js/components/Spinner/index.

js -> new file that contains a basic Spinner component/resources/js/components/index.



jsNow, 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.


jsAnd as the final step, compile the JS assets again:npm run devAfter a successful compile, you’ll now have a functional Laravel React app which can be loaded inside of BigCommerce!Next StepsIf 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 Codehttps://github.


0Now go build something amazing!.. More details

Leave a Reply