Laravel

First basic program using laravel

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.

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:ProgramsXAMPPphp in your terminal/commant prompt and then you enter:

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

This particular commant 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.

you can refer http://www.17educations.com/php/how-to-install-laravel-on-windows/ for installation

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

 

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.

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.

 

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+.