Basic website in laravel 4.2 and styling it with twitter bootstrap
15 January 2015 By Bhavyanshu Parasher
This tutorial is for laravel 4. For laravel 5, visit this
Overview
This is a part of Laravel tutorial series and as I learn new things about laravel, i keep posting them here. Today I spent time learning about laravel views and routes. In previous tutorials, i have covered how to setup laravel on lampp and how to use migrations and seeder. You must read the setup tutorial before going on with this one. If you have already setup laravel, then you can continue reading this.
By the end of this tutorial, we will have a 3 page website with a contact form.
We will just be going through the basics of the following topics in this tutorial:
Let’s begin
Note: This tutorial assumes that you have laravel setup already, if not then please set it up first.
- First we create a basic layout. This basic layout is like a standard template for all the pages with header and footer. Go on and open up your /LARAVEL_PROJECT_ROOT/app/views folder. In that create a file called layout.blade.php. Copy paste the below code in it. The code is pretty understandable as it is basic HTML mixed with laravel’s blade templating code. For example,
{{ HTML::style('css/style.css') }}
creates a tag for including a css resource. Similarly, you can use{{ HTML::style('js/some.js') }}
to include a javascript/jquery resource. Please note that you need to create css and js folders inside your LARAVEL_PROJECT_ROOT/public folder. In those, you need to add the respective css and js files. In the following code, i have already added necessary twitter bootstrap code for you.@yield('content')
defines an area (content) in the template.
-
Next we create content specific to our pages. We would want different content for Home, About and Contact. So we will use
@section('content')
to load these. Create a new file named home.blade.php, about.blade.php and contact.blade.php inside /LARAVEL_PROJECT_ROOT/app/views folder just like you create layout.blade.php. But in this, we will only add sections and nothing else. Edit the content of sections according to your need.home.blade.php -
@extends('layout')
is what makes the template layout.blade.php reusable.@section
defines the content in the area where we@yield
in our layout.@stop
defines the end of section.
about.blade.php - Similar to home page
contact.blade.php - The Form
class provides us very easy to use syntax. We don’t need to code any html for our form. The below code shows how to create a very basic form. The default action for the form is POST. Later you can also view source of the contact page to check the output code. Read more on Forms in the laravel docs.
- Now, open your /LARAVEL_PROJECT_ROOT/app/routes.php. We will edit this file to register routes so that it can properly map our URLs. First one will be for our home page. Second will be an About Us page. On the third, we will have a contact form. The fourth one is use to handle the
post
action of the form. In this, we have set validators to check for data before submiting.
- Now first check if your lampp is running and then go to http://localhost/laravel-project/public and you will see your home page with links. You can try filling the contact form as well and submit it. It will work. If you leave even a single field blank, it will generate an error message. Right click > view source to view the generated source code. Lastly, experiment with it and read the docs.
That’s all. See how simple it is to manage your project. Next we will see how to query database and update contact table in database.
View more tutorials on laravel
blog comments powered by Disqus