Tuesday, July 22, 2014

An Introduction about Laravel Framework

Introduction
Laravel is a very powerful framework, designed for php web application. It will give you ways to modularize your codes and make them look cleaner and easier to read and understand. It is open source software under MIT License.

Features :

Now, we will talk about some prominent features of this framework which make it different from other :
Bundles : These are small packages which you may download to add certain functionality to your web application. Hence, saving a lot of coding stuff and time. These may be containing some custom functionality.
Class Autoloading: It assure that correct components are loaded at correct time and saves a lot of your headache.
View Composer : Theses are block of codes which can run when view is loaded.
Unit testing: Laravel allows user to easily create unit test and run these test making use of Artisan utility.The best thing is it itself supports a number of test to ensure application stability.
The Eloquent ORM: The Eloquent ORM is the most advanced PHP ActiveRecord implementation available. With the capacity to easily apply constraints to both relationships and nested eager-loading you'll have complete control over your data with all of the conveniences of ActiveRecord.
Application Logic : can be implemented within your application either using controllers or directly into route declarations.
Reverse Routing : allows you to create links to named routes. When creating links just use the route's name and Laravel will automatically insert the correct URI. This allows you to change your routes at a later time and Laravel will update all of the relevant links site-wide.
Restful Controllers: are an optional way to separate your GET and POST request logic. In a login example your controller's get_login() action would serve up the form and your controller's post_login() action would accept the posted form, validate, and either redirect to the login form with an error message or redirect your user to their dashboard.
The IoC container (Inversion of Control): Inversion of Control gives you a method for generating new objects and optionally instantiating and referencing singletons. IoC means that you'll rarely ever need to bootstrap any external libraries. It also means that you can access these objects from anywhere in your code without needing to deal with an inflexible monolithic structure.

Installation and Setup of Laravel

Requirements:

  • Apache or any other compatible web server
  • Since, Laravel makes use of PHP 5.3 features so it’s also requirement.
  • For detecting file Mime type Laravel make use of FileInfo Library. Windows users must enable this by making appropriate changes(include the bundled php_fileinfo.dll DLL file in php.ini) to include this extension.
  • It make use of Mcrypt library for security purposes. This library is mostly preinstalled but if you have problems in finding this, then check the vendor site of your LAMP installation

Installation

  • Download Laravel from http://laravel.com/download
  • Extract the Laravel archive and upload the contents to your web server.
  • Set the value of the key option in the config/application.php file to a random, 32 character string.
  • Verify that the storage/views directory is writable.
  • Navigate to your application in a web browser.
Personally, I used Wamp server which included everything needed. This made every requirements fulfillment too easy. You can directly include extensions by making use of GUI.
If everything is fine you must see a welcome page like below :
Welcome Page
Figure 1: Welcome Page

Server Configuration

Laravel tends to protect your codes, bundles by keeping only those files that are necessarily public in the web server's DocumentRoot. This prevents server misconfiguration from getting your codes leaked or accessible to third party.

Basic configuration

All of the configurations provided are located in your applications config/ directory.Make sure you change the 32 character key inside the application.php inside the config folder. Make the key as any random 32 character string. You may automatically generate key making use of Artisan command utility, which we will discuss later.

Environment

Its most likely that the configuration you set for local environment is not same as production. Laravel default environment handling mechanism is URL based, which will make setting up environments very easy. If you open the paths.php from your Laravel installation root directory, you will see the below:
Listing 1: Paths.php
$environments = array(

    'local' => array('http://localhost*', '*.dev'),

);
Here, all the url which are starting with localhost and ending with.dev will be considered under local environment. Now if you create a directory local inside application/config then it will override the options in the base application/config directory. You may create as many environment as you like.

Key terms

Here are some key term which you must be knowing :
Models : Models are the heart of your application. It mainly consist of Business Logic. The models mainly comprises of Database interactions, File I/O operations ,Interacting with web services etc.
Libraries : Libraries are classes that perform tasks that aren't specific to your application. For example a library which can convert your table data to graphs and show that. Creating library is too easy. For eg we create a ShowMess.php with following content and place this inside the library folder:
Listing 2: Library
 
<?php

class ShowMess {

    public static function write($text) {
        echo $text;
    }
}

Now we can call ShowMess::write(“Displaying of text making use of library. This is too simple and easy”) from anywhere in our application which will print “Displaying of text making use of library. This is too simple and easy”.
With Laravel Auto loading feature it is very easy to use both Models and Library.

Troubleshooting

In case of installation problems please check the below points:
  • Make sure the public directory is document root of your web server.
  • If you are using mod_rewrite, set the index option in application/config/application.php to an empty string.
  • Verify that your storage folder and the folders within are writable by your web server.
This is all about an introduction to Laravel. In coming tutorials we will learn a lot about making use of this exciting framework in our web application. Hope you liked the article, see you next time with some more informational article.


No comments:

Post a Comment