Starting a New Project in Laravel

Starting a New Project in Laravel

Continuing the tutorial about Laravel, I will show you how to start a new project. We will install the framework, dependencies, and define the necessary settings. This application is available on Github at github.com/oscardias/desenvolvendo_com_laravel, and with every new article I will create a new tag in the repository to facilitate downloading. The code variables and comments are in portuguese.

Prerequisites

In order to follow and perform the same steps, you will need the following components installed:

  • PHP >= 5.3.7
    • Extensions: MCrypt and JSON
  • MySQL
  • Apache (or another server)
    • mod_rewrite enabled
  • Composer
  • Git (optional)

Installing the Framework

To install Laravel, let’s get the directory structure of our application and a directory called vendor, which contains all the required dependencies, including the framework. This structure contains definitions, standards such as composer.json and .gitignore.

Using the Laravel Installer

The fastest way to make the installation of the framework files and its dependencies is to use the installer provided by the Laravel itself. Download the installer from this link. If you are on Linux or Mac you can rename the file to laravel and put it in /usr/local/bin/. By following this procedure you can create new applications using the command:

For Windows users it is a bit more complicated to use this method. You can include the directory where file laravel.phar is in the PATH of the system or run it directly in the desired folder. For example, if you have a folder called www with your projects, you can place the laravel.phar file in that directory and run the command:

Using Composer

This method is more time consuming than the previous one, but it’s very practical too. Install Composer and run the following command:

Using Git

You can also use Git to clone Laravel’s repository and install the dependencies with Composer. As Composer is quite slow, this procedure is a bit time consuming. Necessary commands:

Downloading

If you prefer you can simply download the latest version at https://github.com/laravel/laravel/archive/master.zip and then use Composer to update the dependencies using composer install.

Settings

Once properly installed, let’s define some settings to access our project.

Virtual Host (optional)

I like to access my projects in the form http://project.localhost/. This allows you to configure .htaccess and the URLs in the same way that will work when the project is in the production domain. There are several ways of doing this and it always depends on your installation. I’ll explain a way for Windows and one for Ubuntu. If you use another OS, search in Google because there are several references on the web.

Windows

On Windows, you should find a file called httpd-vhosts.conf inside the <apache folder>confextra folder. If you do not have this file, you can do it directly in the httpd.conf file. Add at the end of the file:

Replace the directory with the directory where you placed your project. Open the WindowsSystem32driversetchosts file and add a line for the new project:

Restart Apache and try accessing http://laravel_development.localhost. If all went well you should see Laravel’s message “You have arrived”.

Ubuntu

In order to make a virtual host on Ubuntu, create a file laravel_development.localhost.conf within /etc/apache2/sites-available directory (you can use sudo touch to create the file). Put the following content in it:

Remember to replace the directory by the directory of your project. Then run the command sudo a2ensite laravel_development.localhost.conf to activate the VHost. Just like Windows, it is necessary to modify the hosts file. On Ubuntu it is located at /etc/host. Add at the end of the file:

Restart the server using sudo service apache2 reload and you’re all set.

Composer

Let’s modify the file composer.json according to our project. We need to change only the first lines:

Database

Let’s create a MySQL database called dev_laravel. Open the file app/config/database.php and change the array of connections to the following:

Remember to change the user and password for your connection if necessary.

URL

To finalize the basic settings, open the file app/config/app.php and edit the url property. Put the address configured in the virtual host.

Environment

For Laravel to know that we are working on a local environment, we need to open the file bootstrap/start.php and modify the $env variable. You need to add the name of the computer in which you will work locally.

In this case I added my two computers. To find your computer name in Windows, go to Computer > Properties and look for the computer name. On Linux and on Mac just use the hostname command.

In the future this will make it possible to create a local folder inside the config folder. In this folder it will be possible to override the settings that you want to differentiate when you go to production. For example, you can create two different configurations of databases, local and production.

Check if your installation already has a local directory. Possibly there’s a database.php file there. This file will be used by the Laravel when you run locally, so set the database settings in this file as well.

Conclusion

I believe that after this tutorial you will be able to understand a little better Laravel configuration and will be able to follow our next tutorials. Next I’m going to write about the Eloquent ORM. If you have any questions, write in the comments.

This post is part of the Laravel Development series.

Leave a Reply

Your email address will not be published. Required fields are marked *