Skip to main content

Using Foundation 6 with Laravel 5

Laravel comes ready with Twitter bootstrap and Vue.js to get started with our project. But what if we want to use Zurb Foundation?

I am assuming we already know the traditional way of referencing and using foundation via CDN or downloading and referencing it locally. But this tutorial is for those who want to use the power of SCSS in foundation inside Laravel.

As of this writing I am using Laravel 5.4 and Foundation 6.4.1 on windows 10 with XAMPP.

Assuming we have all the necessary installation like php, composer, node.js, npm, git etc., let get started.

Let get started and go through it step by step.




Step 1: Install a Fresh Copy of Laravel

Documentation: https://laravel.com/docs/5.4

$ laravel new foundation_test
$ cd foundation_test

Open the folder with our favorite editor like Sublime Text, Atom Editor or Visual Studio Code.

If we open package.json file, the initial file looks like this

{
  "private": true,
  "scripts": {
    "dev": "npm run development",
    "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch-poll": "npm run watch -- --watch-poll",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "prod": "npm run production",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },
  "devDependencies": {
    "axios": "^0.16.2",
    "bootstrap-sass": "^3.3.7",
    "cross-env": "^5.0.1",
    "jquery": "^3.1.1",
    "laravel-mix": "^1.0",
    "lodash": "^4.17.4",
    "vue": "^2.1.10"
  }
}


Step 2: Install Foundation For Sites via npm

$ npm install foundation-sites --save-dev

After this step we will be able to see the foundation version installed on our package.json file.


"devDependencies": {
  "axios": "^0.16.2",
  "bootstrap-sass": "^3.3.7",
  "cross-env": "^5.0.1",
  "foundation-sites": "^6.4.1",
  "jquery": "^3.1.1",
  "laravel-mix": "^1.0",
  "lodash": "^4.17.4",
  "vue": "^2.1.10"
}


Step 3: Edit 'package.json' File


(a) Open the package.json file on editor and remove the line that contains bootstrap and save the file.

(b) If we look at the foundation installation, foundation works with Jquery version 2. To check this, open the file node_modules/foundation-sites/vendor/jquery/dist/jquery.js

(c) Since we have Jquery 3.1.1 installed by default, lets change the version to 2.1.4 as foundation might have problem with latest version. So after editing, section should look like this.

"devDependencies": {
  "axios": "^0.16.2",
  "cross-env": "^5.0.1",
  "foundation-sites": "^6.4.1",
  "jquery": "^2.1.4",
  "laravel-mix": "^1.0",
  "lodash": "^4.17.4",
  "vue": "^2.1.10"
}


(d) Lets update the packages

$ npm update



Once updated, at least Jquery 2.2.4 will be installed. We are done with this part and lets work on SCSS part now.


Step 4: Set up SCSS Part


(a) If we look at the resources/assets/sass folder, we can find 2 files _variables.scss and app.scss. We can rename app.scss whatever we like (we just have to reflect that change on laravel-mix as well). Let's edit app.scss file.

Remove the lines

// Bootstrap
@import "node_modules/bootstrap-sass/assets/stylesheets/bootstrap";


Replace with

// Foundation
@import "node_modules/foundation-sites/assets/foundation";


(b) Foundation uses global settings file called _settings.scss, in this file we can change any default values set by foundation to suit our needs. Let say we don't care about the customization, we can leave this intact. However if our site theme changes during special times, this file is very handy for tweaking. This file can be found at /node_modules/foundation-sites/scss/settings/_settings.scss.

At this point we can do 2 things, either reference that file in our app.scss directly on node_modules folder or copy it to our resources/assets/sass folder. Later option is more suitable in a situation where we need to do lots to tweaking. Lets copy that file now so that we don't have to dig down to node_modules later when we need it.

(c) copy /node_modules/foundation-sites/scss/settings/_settings.scss to our resources/assets/sass

(d) Reference this file in our `app.scss`

Remove this line

// Variables
@import "variables";


and replace with

// Foundation settings
@import "settings";


At this point we can delete the file _variables.scss


(e) Since we copied _settings.scss file to our convenient location, lets edit this file.

Change this line

@import 'util/util';


with this one, so that it can reference it correctly

@import "node_modules/foundation-sites/scss/util/util";


(f) From this point we can create other SCSS files as needed and import it in our app.scss



Step 5: Setup JS Part


(a) Rename resources/assets/js/bootstrap.js to foundation.js. Change the following:

Change this

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap-sass');
} catch (e) {}


to this one

try {
    window.$ = window.jQuery = require('jquery');

    require('foundation-sites');
} catch (e) {}



$(document).foundation(); // initialize foundation 


 


(b) Open app.js

Change this

require('./bootstrap');


to

require('./foundation');







If foundation doesn't initialize as expected, then initialize on the page like this
.
.
.
<script>
   $(document).foundation(); 
</script>
</body> // before closing body tag
 

At this point we are all set to be using Foundation.




Step 7: Testing

=================

(a) Open resources/views/welcome.blade.php

Add this line after <title> tag
<link href="css/app.css" rel="stylesheet" type="text/css">

Add this line before </body> ending tag.
<script src="js/app.js"></script>


(b) Use some Foundation styling.

Grab this code from Foundation Documentation site http://foundation.zurb.com/sites/docs/button-group.html#coloring

Paste it on our blade file somewhere after <body> tag and save the file.

<body>

  <div class="small 12 columns">
    <div class="button-group">
      <a class="secondary button">View</a>
      <a class="success button">Edit</a>
      <a class="warning button">Share</a>
      <a class="alert button">Delete</a>
    </div>
  </div>
  .
  .
  .


(c) Open the console (Git Bash or similar) on the location of our installation.

$ npm run dev

Let it finish processing

$ php artisan serve


(d) Open browser and go to http://127.0.0.1:8000/. We should see the colored buttons on the page as expected.



Thanks for spending time together. Hope this helps in someway.

Comments

  1. Hey man, really thanks a lot for sharing this! It really helped!

    I have just one issue, javascript is not working now. I took accordion for example, and it doesn't work.. Do you know how to fix that?

    :)

    ReplyDelete
    Replies
    1. You need to initialize the foundation with

      $(document).foundation();

      I have updated the post. Thanks for pointing out

      Delete
  2. Yeah, this does technically get the Foundation styling to work but none of the JS powered components work.

    ReplyDelete
    Replies
    1. Thanks for reminding. I forgot to put foundation initialization code on JS part. Have updated the post with

      $(document).foundation();

      Delete
  3. Replies
    1. Have updated the JS section post with foundation initialization. Sorry for that.

      $(document).foundation();

      Delete
  4. Hey Nice write up, keep Writing, Do you know you can also post your laravel related articles
    on http://www.laravelinterviewquestions.com/submit-an-article too.
    Thanks !

    ReplyDelete
  5. Thanks Man you saved me a couple of hours ;)

    ReplyDelete
  6. Thanks, this was great - fixed a few issues I was having from this tutorial: https://laracasts.com/discuss/channels/tips/using-foundation-630-with-laravel-54

    For those of you complaining about JS not working - add script '$(document).foundation();' before the closing ' tag.

    ReplyDelete
    Replies
    1. Yes you are correct. I have updated the JS section of the post with foundation initialization code. Thanks!

      Delete
  7. Upade: Laravel 5.5 supports front-end presets. However not many presets are shipped with it. I have contributed for some presets for Zurb Foundation, Bulma, uikit to name few. If you are looking for one command to switch presets with auth views for user registration and logins definitely checkout https://packagist.org/packages/laravel-frontend-presets/

    ReplyDelete
  8. Hello!
    As I am learning Laravel Framework I would say this is one of the best articles written on using Foundation 6 with Laravel 5. Thanks for sharing.

    ReplyDelete
  9. It’s a very informative and helpful article, thank you for sharing!

    SEO Services

    ReplyDelete
  10. After reading this blog I am very strong in this topics and this blog is really helpful to all... Explanation are very clear so it is easy to understand.. Thanks for sharing this blog...
    Laravel Development Services

    ReplyDelete
  11. hello ! DevMarkete please visit bellow site and please give me an idea can i buy this gig from fiverr.com Laravel
    https://www.fiverr.com/ahmedwali5990/be-your-laravel-website-expert

    ReplyDelete
  12. Hi,

    Great article...! thanks for sharing your article it will be helpful for me in your article explained step by step i will be use @ here https://www.techtiq.co.uk/PHP | https://www.techtiq.co.uk/laravel

    Regards,

    Daniel

    ReplyDelete
  13. Hi, I did everything in this article but when I create the presets with foundation, grid seems not working. The inputs width from login are taking the page width. I hope you can help me.

    ReplyDelete
  14. Thanks for this information.. it very useful for beginner's

    Laravel Company India

    ReplyDelete
  15. thanks for sharing useful information,

    Providing Laravel Web Developmentl shopping cart, Website Designing and Payment gateway integration.

    Ecommerce Website Development
    Online Business Website Development
    Ecommerce Portal Development

    For more details about my Web Development and Laravel Web Development Services.

    ReplyDelete
  16. Thanks for sharing the useful Blog post about Web Development using Foundation 6 with Laravel 5.

    Web Application Development Company in Coimbatore

    Web Development Company in Coimbatore

    ReplyDelete
  17. Thanks for sharing this information. Nowadays, Laravel is one of the best framework of PHP.You can get the best services from Laravel Development Company.

    ReplyDelete
  18. Yeah!! Laravel is an open-source PHP structure that pursues the MVC design. It is a PHP system that diminishes the expense of advancement and improves code quality. It will enable you to get an incredible line of work. Laravel 5.8 is the most recent and current variant.It will provide necessary support in Laravel Development.

    ReplyDelete
  19. outsourcingall.com "Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it.
    This paragraph gives clear idea for the new viewers of blogging, Thanks you. You’re doing a great job Man, Keep it up.
    Seo training
    Seo training in dhaka
    SEO Services in bangladesh

    ReplyDelete
  20. we are the best custom WordPress Development Company some look like your website.

    ReplyDelete
  21. your way to explain is sach good now i get everything but i have also same like that one blog if you want to read so click here Best Website Designing Company In Delhi

    ReplyDelete
  22. Thank you for writing this informative post on Laravel. Looking forward to read this post.
    Laravel Web Development Services India

    ReplyDelete
  23. Casino - DrmCD
    Visit the official 전주 출장안마 website of the MGM 익산 출장마사지 Gambling Hall of Fame, 태백 출장안마 presented annually by the 화성 출장마사지 South Florida Lottery's Board of Directors on Sunday, December 시흥 출장샵 9, 2021 to

    ReplyDelete
  24. This post is so useful and informative. Keep updating with more information.....
    What Is Laravel Framework
    Laravel PHP Framework

    ReplyDelete
  25. Lottery Management Software is cloud-enabled ERP software that includes tablet, mobile, and web applications that are designed for seamless integration and encryption. Do you have a plan to build top lottery management software for your business? Agnito Technologies is a prominent Online Lottery Software Solution provider company, having years of expertise in delivering outstanding and shameless lottery software, and mobile app platform for our global clients. Our team of expert Lottery Software Developers is well versed in developing fantastic and outstanding lottery software, and app development platforms as per the clients' requirements.

    Lottery Management Software

    ReplyDelete
  26. I was nervous when I Clicked on this on this blog I felt what im looking for this blogs haves but thank you to the author who has everything in this blog for what im looking for. This blog writer has the best knowledge about laravel.

    If you're looking for Laravel Web Development Services in India so, you can visit our CodeKing Solutions for the best offers.

    ReplyDelete
  27. Thank you so much for the informative and interesting post I like your points which you have discussed over here, great job.

    Unity 3D Game Developers

    ReplyDelete
  28. Looking for expert Laravel developers in India? Look no further than Connect Infosoft. Our talented team excels in creating high-performance web applications using the powerful Laravel framework. Hire Laravel Developers in India

    ReplyDelete
  29. Wow, I want to salute you. It's a very good comment and very informative as well
    Kraken clone script

    crypto copy trading software

    ReplyDelete

Post a Comment

Popular posts from this blog

Front-end Presets - A New Feature in Laravel 5.5 Plus Community Supported Preset Options

Laravel 5.4 comes with Bootstrap and Vue.js including Example.vue component for your front-end scaffolding. Depending upon what we are developing, we might just need Bootstrap only but not Vue.js and vice versa or nothing at all. Laravel 5.5 comes with new artisan preset command to switch front-end scaffolding presets easily. Now there is React.js scaffolding included for React lovers. Bootstrap is a CSS framework while Vue.js is JavaScript framework. So artisan preset allows us to switch CSS framework or JavaScript framework separately. NOTE: This scaffolding is intended to be used on fresh install of Laravel before anything. Lets say we want React.js instead of Vue.js, all we have to do is php artisan preset react .  This will neatly switch JavaScript framework from Vue.js to React.js. It also replaces the Example.vue with Example.js React component. If we are working only with Bootstrap but don't want any JavaScript framework scaffolding, we can use php arti