In this tutorial, we are going to look at how to install Laravel, configure Laravel and explore the directories of Laravel. We will work with a windows machine in this tutorial but the knowledge can still be applied to other operating systems such as Linux based OS, Mac OS etc.
We will cover the following topics in this lesson
Before installing Laravel, ensure that you have the following programs already installed
As we started in the above section, it is a package manager that is used to install, uninstall and update existing packages. It is a command line utility program that allows us to type a command in the terminal and watch the magic happen. Composer downloads packages from Packgist.
It is a cool text editor developed by Microsoft that can be used to write code for a number of languages like PHP, Python, C,C++, Java etc. Like we stated in the above section, you can use any text editor that you prefer. Visual Studio Code is cross-platform which means it works on Windows, MacOS and Ubuntu. One of the things that I like about Visual Studio Code is that it comes with a default nice dark theme which is consistent on all platforms and it has great performance. A market place of free extensions also exists. This allows you to activate more features.
You can download Visual Studio Code from the official; website Get Visual Studio Code
We will not cover how to download Visual Studio Code in this tutorial.
We will be working with the command line a lot through out these tutorial series. MacOS and Linux come with good terminals but on Windows the command prompt still works but personally I prefer something more mature. On windows, you can install Git which comes with Git bash.
GitHub has lots of packages and some of the code of for the tutorials will be available on Github. Git will enable you to download the code for the tutorial series as well as code for other applications and packages.
You can download Git from Get Git
In this section, we will look at how to install Laravel version 6 using composer. We will do it from the command line. I will install my project in the home directory inside a directory called
Dev but feel free to use whatever directory that y6ou want.
Open the terminal
Run the following command
cd ~ mkdir Dev cd Dev
We can now install Laravel like so
composer create-project laravel/laravel larashop 6.0.*
composerinvokes the program
create-projectis the command that we use to create new projects.
composerto create a Laravel project.
larashopis the name of our project.
composerto fetch the latest version of Laravel 6.0
Executing the above command should give you results similar to the following
When the project is done install a directory
larashop should exist.
We will now use Visual Studio Code to open the new folder. On windows, you can right-click on the directory
larashop and Select the option
Open with Code
Once you have opened the project directory, you should be able to see something similar to the following.
As you can see from the above image, we have a lot of folders which can be overwhelming if you are a beginner. The good thing is we will be one with only a single directory 80% of the times.
The directory where we will be writing our code in 805 of the times is
The following images shows the
app directory expanded to reveal its contents
Let's now explore the directories. Some of the directories mentioned below do not exist by default but we mention them for the sake of completion.
|1||/app||contains all of your application code|
|2||/app/Console||contains all of your artisan commands|
|3||/app/Events||contains event classes|
|4||/app/Exceptions||contains exception handling classes|
|5||/app/Http||contains controllers, filters, and requests|
|6||/app/Jobs||contains jobs that can be queued|
|7||/app/Listeners||contains handler classes for events|
|8||/app/Policies||Contains authorization policy classes|
|9||/bootstrap||contains files required by the bootstrap framework|
|10||/config||contains the application configuration files|
|11||/database||Contains database migrations and seeds. It is also used to store the database for SQLite|
|13||/resources||Contains views, localization files and raw assets|
|14||/routes||Contains files that define routes for web, api, console etc.|
|14||/storage||contains compiled blade templates, filed based sessions, etc.|
|15||/tests||contains automated unit tests|
|16||/vendor||contains composer dependencies|
Now that we have explored the directories of Laravel, let's configure our application.
The configuration information is stored in the
.env file in the project root. Laravel uses
/config/app.php to check for configuration parameters defined in
In this section, we are going to;
Note: Laravel comes with sensible defaults out of the box. We won’t have to change some of the settings.
Let’s start with the
.env file in the root directory
Locate the following lines
By default, APP_DEBUG is set to true. If you want to turn it off, you can set it to false but for now, we will leave it set to true. When you deploy the application to production,
APP_DEBUG should always be set to
Let’s now look at how Laravel reads this value
Open the file
Locate the following code
'debug' => env('APP_DEBUG', false),
'debug' => env('APP_DEBUG', false),Laravel reads the value of the key
.envfile. If it is not found, Laravel will use
falseas the fall-back value. The value is assigned to the
debugkey of the configuration array.
Locate the following code
'timezone' => 'UTC',
'timezone' => 'UTC', sets the time zone to UTC. This is the default value
If you would like to have a different time zone, you can replace UTC with a value of your preferred time zone. You can fine a list of supported time zones here
Locate the following code
'key' => env('APP_KEY'),
'key' => env('APP_KEY'),Laravel reads the application key from our
.envconfiguration file. This key is automatically generated when you create a new Laravel application. It is used for encryption purposes and must be kept secret.
The application name is used in a number of templates within the application. For example, when you send an email from the application using default templates, The name of the application is included in the email. The default value is Laravel so we can change it to a name that is specific to our application.
Locate the following line
Change it to the following
You can explore
/config/app.php to see the various settings that are available in it.
The authentication configuration file is located in
/config/auth.php. We will leave the default values as they are. If you want you can change them to meet your requirements.
The database configuration file is located in
/config/database.php. By default, MySQL will be used as the database engine. You can set it to a different database management system if you want but for this tutorial, we will leave the default value. The code for database configuration looks like the following
'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : , ],
'driver' => 'mysql',sets the database driver name to
'url' => env('DATABASE_URL'),reads the database URL from the
.envfile and assigns the value to the array key
'host' => env('DB_HOST', '127.0.0.1'),sets the database host name or IP address based on the values set in the
'port' => env('DB_PORT', '3306'),sets the database port which defaults to
3306. if you are running MySQL on a different port number then you can update this value in the
'database' => env('DB_DATABASE', 'forge'),sets the database name
'username' => env('DB_USERNAME', 'forge'),sets the database username. This should be a valid user name that exists in MySQL.
'password' => env('DB_PASSWORD', ''),sets the database password. This should be a valid password that you use to login to MySQL.
other settingsthe rest of the settings are defaults that are rarely changed so we will not discuss them yet.
In this tutorial, we looked at;
Let’s get our hands dirty by developing a simple hello world application. We will continue building on this project. Use the next tutorial button to navigate to the next tutorial