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.
$ 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"
}
}
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"
}
(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.
(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
(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.
(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.
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-devAfter 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.
Hey man, really thanks a lot for sharing this! It really helped!
ReplyDeleteI 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?
:)
You need to initialize the foundation with
Delete$(document).foundation();
I have updated the post. Thanks for pointing out
Yeah, this does technically get the Foundation styling to work but none of the JS powered components work.
ReplyDeleteThanks for reminding. I forgot to put foundation initialization code on JS part. Have updated the post with
Delete$(document).foundation();
JS doesn't work.
ReplyDeleteHave updated the JS section post with foundation initialization. Sorry for that.
Delete$(document).foundation();
Hey Nice write up, keep Writing, Do you know you can also post your laravel related articles
ReplyDeleteon http://www.laravelinterviewquestions.com/submit-an-article too.
Thanks !
Thanks Man you saved me a couple of hours ;)
ReplyDeleteThanks, 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
ReplyDeleteFor those of you complaining about JS not working - add script '$(document).foundation();' before the closing ' tag.
Yes you are correct. I have updated the JS section of the post with foundation initialization code. Thanks!
DeleteUpade: 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/
ReplyDeleteHello!
ReplyDeleteAs 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.
It’s a very informative and helpful article, thank you for sharing!
ReplyDeleteSEO Services
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...
ReplyDeleteLaravel Development Services
hello ! DevMarkete please visit bellow site and please give me an idea can i buy this gig from fiverr.com Laravel
ReplyDeletehttps://www.fiverr.com/ahmedwali5990/be-your-laravel-website-expert
Hi,
ReplyDeleteGreat 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
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.
ReplyDeleteThanks for this information.. it very useful for beginner's
ReplyDeleteLaravel Company India
thanks for sharing useful information,
ReplyDeleteProviding 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.
Thanks for sharing the useful Blog post about Web Development using Foundation 6 with Laravel 5.
ReplyDeleteWeb Application Development Company in Coimbatore
Web Development Company in Coimbatore
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.
ReplyDeleteTHANK YOU!
ReplyDeleteYeah!! 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.
ReplyDeleteoutsourcingall.com "Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it.
ReplyDeleteThis 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
Thanks for sharing this content!
ReplyDeletewordpress development company
we are the best custom WordPress Development Company some look like your website.
ReplyDeleteyour 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
ReplyDeleteReally very informative and creative contents. This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up. Data Extraction Services
ReplyDeletePHP Services
Ecommerce Service Provider
Digital payment gateway
Digital Payment Platform
Bulk Appointments registers
Online Appointment Scheduling Software
Thank you for writing this informative post on Laravel. Looking forward to read this post.
ReplyDeleteLaravel Web Development Services India
Thank you man, helped a lot!
ReplyDeleteNice blog. Thanks for sharing such a great information!
ReplyDeleteBest laravel open source ecommerce platform
Amazing blog. I have never seen a blog like this
ReplyDeleteLaravel Devlopment company
Casino - DrmCD
ReplyDeleteVisit 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
This post is so useful and informative. Keep updating with more information.....
ReplyDeleteWhat Is Laravel Framework
Laravel PHP Framework
Magento development agency
ReplyDeleteLottery 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.
ReplyDeleteLottery Management Software
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.
ReplyDeleteIf you're looking for Laravel Web Development Services in India so, you can visit our CodeKing Solutions for the best offers.
Thank you so much for the informative and interesting post I like your points which you have discussed over here, great job.
ReplyDeleteUnity 3D Game Developers
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
ReplyDeleteWow, I want to salute you. It's a very good comment and very informative as well
ReplyDeleteKraken clone script
crypto copy trading software
Good Knowledge Share
ReplyDeleteMetaverse Development Company
Thanks for sharing this Post!!!
ReplyDeleteCryptocurrency Exchange Script
Binance Clone Script
P2P Cryptocurrency Exchange Software