January 5, 2015 6:08 am

Laravel 4 Basics Part 1

Hello Friends Today I am writing about Laravel Basics a very demanding framework first article on PHPGang.com. Larvael is very fast growing PHP Framework and very easy to learn and use. In this tutorial we are going to write how to install Laravel and configure it and make your first application. We divided this tutorial in 2 parts so let see its first part.

laravel Basics part 1

[wpdm_file id=122]

Installation

To install Laravel first you need to download and set up Composer. If you are using Windows you can grab the .exe file and run it which will set Composer up automatically.

For other operating systems, you should follow the instructions at: https://getcomposer.org/download/
To set up your first Laravel project you browse to the folder where composer has been installed (with something like cd E:\Programs\XAMPP\php in your terminal/command prompt and then you enter:

php composer create-project laravel/laravel E:\LaravelBasics\ –prefer-dist

This particular command is telling composer to create a template of a project using the Laravel framework and set it up in the Laravel Basics folder of E: drive.

This will create all necessary files for a Laravel application.

Laravel Books:

See Also: Laravel 4 Basics Part 2:  Refactoring and Finishing the Basic Application

Creating a Basic Application using Laravel

Users will see the public folder.

We have moved the LaravelBasics folder but you can see the path to access the application:

http://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/

The first thing that you have to do when starting to develop a Laravel application from scratch is to set Laravel to show errors. If you do not set this up you will always get “Whoops, looks like something went wrong” when there is something wrong with your code.

To do this, open app/config/app.php and set ‘debug’ to true.

Now open app/routes.php

Routes basically tell the application which controller should take a particular URL request or process it themselves by interacting with the model and the view (the view is what is going to be displayed to the user)

Add this route to the routes.php

Route::get('guess/{numbers?}', function($numbers=6)
{
	//Generate a random number between 0 and 20
	$luckyNumber= mt_rand(0,20);
	//Create an array with numbers
	$numbers=explode(",", $numbers);
	if (count($numbers) >5) {
		// Send to the view only the first 5 numbers
		$numbers=array_slice($numbers, 0, 5);
	}
	return View::make('guess')->withNumbers($numbers)->with("lucky", $luckyNumber);
});

This basically says that if the URL is

http://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/guess/10,12,15

or

http://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/guess/16 the file called guess.php/guess.blade.php has to be shown to the user (you do not have to write the file extension in the View::make()’s argument. A text in {} (curly brackets) means that a random input may follow the request (guess/) in the URL and ? means that it is optional for the URL request to have the random input for which the ? applies. Thus, if we open the URL without providing a number like that:

http://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/guess the view will load with the default number that we have given (6).

If we write Route::get(‘guess/numbers/’)… then only http://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/guess/number/ would lead to displaying the ‘guess’ view.

withNumbers($number); is a magic method, it is the same as with(‘numbers’, $number) meaning that the View ‘guess’ would be able to use the $numbers variable from the route/URL whenever it wants to use it. It could be explained as with(‘variable name in the view’, ‘variable to pass to the view’).

Then in app/views we create a file called guess.blade.php to make use of the Blade Templating Engine.

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Lucky Number Guesser</title>
	</head>
	<body style="font-size: 1.2em;">
	

		<div style="text-align: center;background-color: #eee;">
		<h1> Did you guess the lucky number?</h1>
		<p>You can try maximum 5 numbers in a comma-separated listand the actual lucky number is between 0and20</p>

		@foreach ($numbersas$number)
			@if ($number==$lucky) 
			<h1 style='background-color: lime;color:#000;padding:10px;'> You said {{{ $number }}}. You guessed the lucky number! Jackpot! The lucky number was {{ $lucky }} </h1>
			@else
				<h1 style='background-color: crimson;color:#fff;padding:10px;'> You said {{{ $number }}}. You did not manage to guess the lucky number. The lucky number was {{ $lucky }} </h1>
			@endif;
			



		@endforeach;

		</div>

	</body>
</html>

In the Blade’s templating engine {{ }} (double curly brackets) work just like PHP’s echo or print statements and {{{ }}} (triple curly brackets works the same way but they also sanitize the input).

You can see a quick demo in the code above of how to use if/else statements and foreach loops using Blade.

Now if we type an URL like:

http://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/guess/1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 only our first 5 could-be-lucky numbers will be compared against the real lucky number.

laravel basics demo

That’s all for the part 1 of the Laravel Basics and its 2nd part will be updated on next week. Please share your problems in comments a download code file is available you can easily download it from website. If you like our article then please share it with your friends on Twitter, Facebook and Google+.

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories:

2 responses to “Laravel 4 Basics Part 1”

  1. sudhakar says:

    nice article … i will star today laravel , now i am a codeigniter developer i will try to move laravel your article is very nice explanation i will try to install if getting any issue i will approach you thank you.

  2. Kumar Ramalingam says:

    Good. Am beginner for laravel framework and need for more examples about routing.

    Thanks Ivan Dimov.

Leave a Reply

Your email address will not be published. Required fields are marked *