Get Started With Laravel


Laravel has evolved a lot in the past few months. If you had to migrate from a Laravel 4.0 application to La Ravel 5.* you would know the difference. It is very powerful framework now. It is my favorite PHP framework when comes to personal projects(PS: I still use Symfony for quite a lot of work that I do for my clients if the language chosen is PHP).

Here we will discuss how to install and get started with Laravel 5.3.

Before we proceed further please make sure that your PHP installation is above 5.6.4 and you have composer installed. You will also need to have the following extensions enabled:

  • OpenSSL
  • PDO
  • mbstring
  • tokenizer
  • xml

The recommended method is to install via the Laravel installer. However, I prefer creating Laravel projects using composer.

Open the terminal and type:

composer create-project --prefer-dist laravel/laravel my_project

This will create a Laravel project and its dependencies.

Once you have done this, you can go to the directory my_project and issue:

php artisan serve --port 8080

Now visit http://localhost:8080 and you can see your Laravel application.

Installing Homestead

If you are trying to create a simple project or just experimenting Laravel you can simply use the artisan serve method we discussed above. However, if you wish to create a professional development environment for developing a Laravel based application then you should use Homestead. So, let’s set that up. However, before you proceed to install Homestead, please make sure that you have vagrant installed.

You can install Homestead by simply issuing:

vagrant box add laravel/homestead

I prefer a different approach. To have the latest Homestead box, clone the latest master branch from github. You can do this by:

git clone https://github.com/laravel/homestead.git Homestead

Once you have cloned the code, go to the Homestead directory and issue

bash init.sh

This will set up Homestead for you.

Configuring Homestead

Open Homestead.yml in the Homestead directory and update the following values:

provider: virtualbox
folders:
    - map: ~/my_project
      to: /home/vagrant/Code
sites:
    - map: project.app
      to: /home/vagrant/Code/Laravel/public

This assumes that you have created the Laravel project in your home directory and you are using virtualbox as the virtualization provider for your vagrant.

Now edit your hosts file:

192.168.10.10  project.app

Now visit http://project.app - voila! Your application is up and running.

Laravel Directory Structure

The directory structure of Laravel is straight forward. As you are a beginner you need to focus only on the following directories:

- app : All core files related to the application go here. This includes controllers, events, jobs etc. - routes: All the routing information associated with your application should be placed under this directory. - resources : This is the place for all views and assets. - public: This is a publicly accessible directory.

Under the app directory you need to focus only on Controllers to get you started.

As mentioned before, all the routing for the application is done using the files under the routes folder. Here, as you are a beginner, you need to focus only on the web.php file.

Artisan

Laravel is a beautiful framework as far a web developer is concerned. One of the main features of Laravel and one that makes it beautiful is the artisan command that comes with it. Using this you can create models,controllers, events, jobs etc. You can even create custom commands by extending it (Please note that you are essentially extending Symfony's Command class.

If you wish to create a new controller you can do that by issuing:

php artisan make:controller MyController

Now imagine you want to create a controller that needs to handle rest operations. You can do that by running:

php artisan make:controller MyController --resource

I will soon be adding an article that explains how to create your first Laravel application.