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.
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
1
2
3
4
5
6
7
8
9
10
11
12
|
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 onlyhttp://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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<!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.
To download the code associated with this article, visit the original place where the article was published: http://www.phpgang.com/laravel-4-basics-part-1_843.html
Be First to Comment