Building Your First Laravel Application – Step by Step

Laravel is a PHP framework which describes itself as a A Framework For Web Artisans. According to Laravel strives to bring back the joy to programming by making it simple, elegant, and, most importantly, well-documented.

From my experience with the framework, I would definitely agree that Laravel hits these basic three points dead-on:

  • Simple – Laravel’s functionalities are easy to understand and implement even for junior programmers.
  • Elegant – Most of Laravel’s functionalities work seamlessly with very little configuration therefore relying on standard conventions to lessen code-bloat
  • Well-documented – Laravel’s documentation is complete and it’s also always up-to-date.

Throughout this tutorial, we’ll be creating our very own app called vrsoftcoder. This little app lets you post, like, and comment on photos, as well as follow other users. Without further ado, let’s dive in!

Before we can even begin, let’s ensure that we do have a system that can support Laravel. Laravel requires the following:

  • AT least PHP 5.3.x – Laravel makes use of a lot of PHP specific features, like closures, late-static binding and namespaces.
  • The FileInfo library – this is enabled in PHP, but on Windows, you will need to add the extension in your PHP.ini configuration file.
  • Mcrypt library – this is used by Laravel for encryption and hash generation, and will typically come pre-installed with PHP.

Once we’re done setting up the environment, let’s download Laravel and all the libraries we’ll be using for vrsoftcoder. Download the following files and place them within a web-accessible folder:

Go to Laravel’s root folder, you should find a public folder – this is where all publicly accessible files should be stored. Laravel has some premade folders inside the public folder for our assets, cssimg, and js folders. Place the Twitter Bootstrap and jQuery files in their corresponding folders. Your folder structure should look like this:

Twitter Bootstrap will have some files inside the css, img, and js folders while jQuery will be inside the js folder.

Before we can write any code, we will need to set an encryption key for Laravel to use for our application. Laravel uses this to encrypt any data we might need encryption for, like cookies. Now open the application/config/application.php file and find the key setting. The default value will be YourSecretKeyGoesHere!. Set it to blank; it should look like:

01
02
03
04
05
06
07
08
09
10
11
12
13
/*
|--------------------------------------------------------------------------
| Application Key
|--------------------------------------------------------------------------
|
| This key is used by the encryption and cookie classes to generate secure
| encrypted strings and hashes. It is extremely important that this key
| remain secret and should not be shared with anyone. Make it about 32
| characters of random gibberish.
|
*/
'key' => '',

Let’s now open up a Terminal/Command Prompt. Browse to Laravel’s root directory, and use Artisan to generate a key:

php artisan key:generate

This should now automatically generate a random 32 character encryption key for Laravel. If you have done it correctly, it should now look like:

01
02
03
04
05
06
07
08
09
10
11
12
13
/*
|--------------------------------------------------------------------------
| Application Key
|--------------------------------------------------------------------------
|
| This key is used by the encryption and cookie classes to generate secure
| encrypted strings and hashes. It is extremely important that this key
| remain secret and should not be shared with anyone. Make it about 32
| characters of random gibberish.
|
*/
'key' => 'e9Vo0SQDe5HjsvXWcTogqKluVfEqnfEu',

If you may need to regenerate your key, just repeat the steps!

We now need to change the configuration so that it can accept pretty-urls and work even without pointing out our requests to index.php. In the application/config/application.php file, look for the following:

01
02
03
04
05
06
07
08
09
10
11
12
/*
|--------------------------------------------------------------------------
| Application Index
|--------------------------------------------------------------------------
|
| If you are including the "index.php" in your URLs, you can ignore this.
| However, if you are using mod_rewrite to get cleaner URLs, just set
| this option to an empty string and we'll take care of the rest.
|
*/
'index' => 'index.php',

If we want to enable pretty-urls, just make sure that the index setting is blank, like:

01
02
03
04
05
06
07
08
09
10
11
12
/*
|--------------------------------------------------------------------------
| Application Index
|--------------------------------------------------------------------------
|
| If you are including the "index.php" in your URLs, you can ignore this.
| However, if you are using mod_rewrite to get cleaner URLs, just set
| this option to an empty string and we'll take care of the rest.
|
*/
'index' => '',

If you’re using this, you must ensure that you have mod_rewrite enabled on your web server (that is if you’re on Apache).

Last but not least, we’ll setup a virtual host for Laravel. This isn’t necessary in a development environment, but, for production, it’s important that we don’t allow access to the Laravel library and application files. As mentioned above, in the Laravel files you should see a folder, named public, which is where all publicly accessible files should go. In addition to that, we need to make sure that our domain, only points to the public folder and nowhere else.

Configuring a virtual host usually depends on which web server you’re using. Here’s an example for the Apache Web Server:

1
2
3
4
5
6
<VirtualHost *:80>
    ServerName vrsoftcoder.com
    DocumentRoot "D:/Development/htdocs/vrsoftcoder/public"
    <Directory "D:/Development/htdocs/vrsoftcoder/public"> 
    </Directory>
</VirtualHost>

We should also add the domain, vrsoftcoder.com, to our hosts files (since this domain doesn’t really exist). In Windows, edit the file C:\Windows\System32\drivers\etc\hosts; in Linux/OSX, you usually edit /etc/hosts. Add this line to the file:

127.0.0.1 vrsoftcoder.com

This will inform our machine that the domain  will resolve to 127.0.0.1, which is the local computer.

 

Post Created 5

Leave a Reply

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top