Laravel - Setting up Bootstrap

Getting Started

First we're are going to create a new project and add it to our localhost. I'm gonna name it mysite and in localhost mysite.dev. If you have no idea how to create a new project and on how to add it to localhost, please refer to my tutorial about Installing Laravel.

Installing Node.js

Next, we're going to use node.js to handle the development dependecies of laravel. Use this link to download node.js and select the recommended version.

Installing our Dependencies

1. Open command prompt and navigate to your project directory. In my case, in C:\xammp7\htdocs\mysite. 2. Type npm install. It will download all the necessary dependencies like bootstrap-sass, jquery, etc. npminstall npminstall

Setting up our Routes

To test if we have installed bootstrap, we're going to create a simple navbar that contains our Home, About and Contact page. Go to routes folder and open in your text editor web.php. You can see that the default route is this:
  1. Route::get('/', function () {
  2.     return view('welcome');
  3. });
Change this into the ff codes:
  1. Route::get('/', function () {
  2.     return view('home');
  3. });
  4.  
  5. Route::get('/about', function () {
  6.     return view('about');
  7. });
  8.  
  9. Route::get('/contact', function () {
  10.     return view('contact');
  11. });
This will create our routes with home as the index. As you can see, the route will return view then the name of the file. It means that it will return in views folder with the file name home.blade.php for home route.

Setting up our Views

Go to views folder and create the ff files: home.blade.php
  1. @extends('layout.app')
  2.  
  3. @section('content')
  4. <h1>Home</h1>
  5. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  6. tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  7. quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  8. consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  9. cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  10. proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  11. @endsection
about.blade.php
  1. @extends('layout.app')
  2.  
  3. @section('content')
  4. <h1>About</h1>
  5. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  6. tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  7. quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  8. consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  9. cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  10. proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  11. @endsection
contact.blade.php
  1. @extends('layout.app')
  2.  
  3. @section('content')
  4. <h1>Contact</h1>
  5. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  6. tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  7. quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  8. consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  9. cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  10. proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  11. @endsection
Notice that in this code @extends('layout.app'), it extends in layout folder in file name app.blade.php. So we're gonna make that file.

Creating our Extends and Includes

In views folder, create another folder and name it as layout then create a file inside it and name it as app.blade.php app.blade.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.         <meta charset="utf-8">
  5.         <title>Laravel Sample Site</title>
  6.         <link rel="stylesheet" href="/css/app.css">
  7. </head>
  8. <body>
  9. @include('inc.navbar')
  10. <div class="container">
  11.         @if(Request::is('/'))
  12.                 @include('inc.showcase')
  13.         @endif
  14.         @yield('content')
  15. </div>
  16. </body>
  17. </html>
Notice that we have include navbar.blade.php and showcase.blade.php located in inc folder. To create this files, in views folder, create a new folder named inc and inside it create a new file named navbar.blade.php and another file named showcase.blade.php. navbar.blade.php
  1. <nav class="navbar navbar-inverse">
  2.         <div class="container">
  3.                 <div class="navbar-header">
  4.                         <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
  5.                                 <span class="sr-only">Toggle navigation</span>
  6.                     <span class="icon-bar"></span>
  7.                     <span class="icon-bar"></span>
  8.                     <span class="icon-bar"></span>
  9.                 </button>
  10.                 <a class="navbar-brand" href="#">MySite.Dev</a>
  11.         </div>
  12.         <div id="navbar" class="collapse navbar-collapse">
  13.                 <ul class="nav navbar-nav">
  14.                         <li class="{{ (Request::is('/') ? 'active' : '') }}"><a href="/">Home</a></li>
  15.                 <li class="{{ (Request::is('about') ? 'active' : '') }}"><a href="/about">About</a></li>
  16.                 <li class="{{ (Request::is('contact') ? 'active' : '') }}"><a href="/contact">Contact</a></li>
  17.                 </ul>
  18.         </div>
  19.         </div>
  20. </nav>
showcase.blade.php
  1. <div class="jumbotron text-center">
  2.         <div class="container">
  3.                 <h1>Welcome to MySite</h1>
  4.                 <p class="lead">This is a Laravel Powered Site. This site uses Laravel version 5.5.</p>
  5.         </div>
  6. </div>
Your file structure should look like this: filestructure

Running our Project

Go to your web browser and type your project. In my case mysite.dev and it should look like this. bootstraplaravel That ends this tutorial. Happy Coding :)

Comments

Add new comment