Your First Larvel App - Installing Composer and Creating a Project

There are a couple of different ways of creating a new laravel project - both of them require Composer. Composer, if you're not already familiar with it, is a dependency package management system. If you have some experience developing software it's likely you've come across dependency package management systems before. If this is the case you can skip the next few paragraphs. If you haven't come across them you will need at least a basic understanding of how they work in order to progress.

Modern software systems tend to be an assemblage of interdependent components. This is great - but complex. Component A won't work unless component B is loaded as it has it uses Component B. Component B won't work work unless C is loaded. Add the different versions of the various components into this mix and managing the inter-dependencies becomes a nightmare. This is where package/dependency management comes in. Every language has it's preferred (or couple of preferred) package management systems. Java has Maven, Javascript has npm and bower and PHP has Composer and PEAR. PEAR has fallen away somewhat in terms of popluarity - Composer is currently the most commonly used system. Once you get comfortable with them, package managers are great. They massively simplify a very complex problem. The only downside is that when you're getting started they add an extra layer of complexity which can be a bit daunting.

Composer is a set of PHP commands that are designed to be run on the command line. From here on out we'll refer to the command line as the CLI (Command Line Interface). Linux/Unix programmers tend to live very happily on the command line interface. For many Windows programmers its a new experience. Once you get used to it, and you know the command to use, the CLI is generally faster and easier to use. For our purposes, there's an additional benefit in that you can quickly and easily copy and paste commands from this blog into your command line in order to run them. In this way we can get moving very quickly.

Open a Windows CLI by holding down the Windows Key and R. When the "run" window comes up, type "cmd" and press enter. This brings up the dos command shell (the CLI). I generally place my laravel folder in the root directory of my C:\ drive but you can put it elsewhere if you prefer - or if you are restricted. To "change directory" into the root folder of the c drive type

cd \

Make a new folder for laravel projects called laravel by typing

md laravel

md stands for make directory.

To change directory into this new laravel folder type

cd laravel

To install Composer on Windows you can use the instructions from the following page

https://getcomposer.org/download/

Here's a screenshot of what that will look like.

Having installed Composer you can use it to either (i)download and install a laravel installer which you can then use every time you create a new Laravel project or (ii) Use composer to create the Laravel project from scratch. I prefer the second of these approaches. Even though creating the project in this way takes a bit longer, I have had problems with the first approach particularly in Windows and if you don't have a standard setup or you don't have admin access (as is often the case for my students) then the second approach is more straightforward and likely to work without exceptions.

Before you can run the composer command which will generate your new laravel project you need to ensure certain PHP extensions will be loaded by PHP. Composer requires the openssl extension, and the mbstring extension. When we start to use Laravel itself it will require the pdo_mysql extension. To ensure these extensions load when PHP is run open your php.ini file in your PHP folder and make sure the following lines are not commented out. (comments in the ini file start with a semi-colon). My edited php.ini file appears as follows

To create your new laravel project type the following

php composer.phar create-project --prefer-dist laravel/laravel tennisClub "^8.0"

Note that the above command will install the most recent version of Laravel which corresponds to your PHP version. If, for example, you are using PHP version 7.3.x you will (at the time of writing) get Larvel version 6.5.2. If, you are a little behind with your version of PHP and were running, say, PHP 7.1.4. You would get Laravel 5.8.

Note also that in the above command we are using a phar file. A phar file is a PHP Archive file much like a Java Archive (jar file). If we were planning on using composer a lot it might make sense to install it in such a way as to allow composer to have it's own command which would run from the command line. Other examples you will see simply call composer create-project or composer update without the need for PHP in the command. We may get to this at a later stage but for now we'll stick to doing the bare minimum in order to get a Laravel app up and running.

Next, in order to scaffold/generate Models, Views and Controllers for our Tennis Club application from the database we need to install an Admin Panel Generator.

Leave a Reply