Laravel is the most popular PHP framework among the web developer community right now. If you need to implement a web application using PHP and following the MVC architectural pattern (MVC: Model-View-Controller) with expressive, elegant syntax, Laravel is the best-recommended option for you. Laravel is free, open-source, accessible, yet powerful, providing powerful tools needed for large, robust applications.
The predecessor of Laravel is Symfony, which is another great PHP framework. Although Symfony is considered as the industry standard and best-known for its larger community support, within next couple of years time, it will fade away with the rising of Laravel lovers.
If you are a computer science student or someone just more than an ordinary coder, you will love seeing the OOP (Object-Oriented Programming), MVC, modular development, query building, application logic, data modelling, version controlling, testing support, and several other great implications used in Laravel. On top of everything, it is easy to learn and apply; ‘rapid development’ they say. But if your motive is to learn PHP from scratch and discover many great things related to PHP, using a framework is not a very good idea. You will not learn pure PHP by using Laravel. You can easily implement your semester project or any industry work with Laravel. What you must thoroughly understand is, Laravel is a collection of development features, not just pure PHP coding.
Before getting started with Laravel, let’s check the pre-requisites for our development work.
You must have installed the following development tools in your local environment.
- XAMPP (Cross-platform Apache MySQL PHP Perl ) / WAMP (Windows Apache MySQL PHP) / LAMP (Linux Apache MySQL PHP) : To run and test any PHP project on your local machine, you need a web server software which can convert your computer into a server and provide the ability to host your files locally. XAMPP can be installed and easily configured with the instructions here.
- phpMyAdmin : If you are using XAMPP, you will get phpMyAdmin with XAMPP installation package. Unfortunately, LAMP users will have to install phpMyAdmin manually to their machines.
- Composer : Composer is a popular dependency management tool for Laravel. In simple terms, it has the capability to read a file (composer.json, composer.lock) in your Laravel directory and download the essential dependencies for your project.
- Git : Git is a popular version control system used by the professionals all over the world. (Git was originally created by Linus Torvalds in 2005 for development of the Linux kernel, with other kernel developers contributing to its initial development.) Although Git is not very necessary for the basic implementation of Laravel, you will face lots of problems during the development life cycle if you ignore this requirement. Also, you must note that Git is a highly-recommended tool for any kind of development work. During the Git installation, make sure you enable both Command Prompt access and Bash access of Git commands.
1) Create a remote repository for your project.
Although we can use Git on your local isolated machine, let’s use a web-based Git repository hosting service to keep your code safe online and take it everywhere you go. Using an online repository lets the users have access to your code online, download and develop it on another machine, add team mates and encourage collaborative development, and have many benefits at the later stages of the development cycle. There exist two very famous hosting services which support Git version controlling: Github and BitBucket. Github is my favorite. You cannot simply find a developer without a Github account, it is that famous.
Go create a free account on any platform you prefer, create a repository for your project. Give it a nice name. (In the context of software naming, nice means adherence to the basic naming conventions. eg:- Don’t use spaces because it can cause problems when performing the terminal operations) Also, make sure you select whether you need your code to be available in private or public.
If you have a development team, send invitations for them to join your project as contributors.
2) Clone the remote repository to your local machine.
It is hard to make changes to your project online. So let’s take a copy to your local machine for development purposes.
As you all know, using XAMPP (or a similar local server), you can only access to the files which are located inside the htdocs directory (~/xampp/htdocs/). Therefore first of all, go to this directory using your terminal (Command Prompt, Git Bash or Linux Terminal). Git commands have no difference on any terminal. But other general commands have variations for Windows Command Prompt and Linux Terminal. Git Bash also uses the Linux commands. But it is a light weight terminal in comparison to Linux Terminal. After going into the htdocs directory using your terminal, you can clone the repo using this command,
git clone https://github.com/ashenwgt/management_system.git
You may now wonder what this github link does. It is the path to your online repository, i.e. the URL. You can find this link after opening your online repository on your Github account. To verify the action, go to the htdocs and check the newly-created directory for your project.
3) Install Laravel.
One can refer the Laravel documentation and setup Laravel using Laravel Installer and Composer. But since this methodology has platform-dependent variations, I recommend you to use the developer approach to this.
First we need to download the Laravel repo from Github (See, Laravel itself uses Github to keep the code). Make sure you choose the master branch and then download the repo as a .zip file to your desktop or the default download location.
Now extract the .zip file to your cloned directory. In my case, the extract destination looks like ~/xampp/htdocs/management_system/.
4) Create all dependencies using Composer.
If you carefully look at the Laravel online repository and the documentation, you can notice how often the technologies are changing over the time. Therefore keeping all vendor files (libraries, bug fixes, latest updates) with Laravel installation folder is not a good idea because the user might miss the latest changes. (Also, the installation folder might be unnecessarily large) To overcome this issue, only a list of dependencies is kept inside the Laravel installation folder. When time comes for the usage, we can download the recommended vendor files inside the Laravel folder using the dependency management tool called Composer. You can get these vendor files using this command;
This command will install all the dependencies as specified in the composer.lock file. To verify the action, check the newly-created vendor directory in your Laravel directory.
Installation part is now over. Before getting started with our development work, we have to make few configurations in the development environment.
5) Configure environment settings and generate Application Key.
In your Laravel folder, you can see the .env.example file. Actually this configuration file is there only for the reference purposes of the developer. To run Laravel, you need a .env file inside the Laravel folder, which is something you need to add manually. The content in the .env file is very similar to the content in the .env.example file. So let’s first copy the .env.example file and then edit its content to get the .env file for our use. To copy the file, run this command,
For Linux/Git Bash: cp .env.example .env
For Windows : copy .env.example .env
Next we have to assign a random string as our application key in the .env environment file. Typically this string should be 32-characters long.
(If you open the .env file, you can notice that the default string assigned to APP_KEY is “SomeRandomString” which is only 16-characters long. You will wonder why 32 characters are needed. That is because there is a rule defined in Laravel’s config/app.php file as so: ‘cipher’ => ‘AES-256-CBC’. An advanced user can manipulate these values inside Laravel’s config directory as per the requirement.)
To generate a valid string for APP_KEY, run this command,
php artisan key:generate
The command should automatically update the app.php file (and also the .env file) with a new proper-size key, and output the generated key on your terminal. You can verify that this key is now in Laravel’s .env file. If it is not there, you will have to manually replace the 16-char-long string with a new 32-char-long string you like and save the changes in .env file.
The basic configurations are now over. You can run the Laravel installation on your browser and see whether it is working fine. Open your browser, go to http://localhost/management_system/public/ and you will see the Laravel welcome screen, if the installation is successful.
6) Create MySQL database using phpMyAdmin.
As we all know, we use tables to store data in a database. Let’s setup a database connection for our Laravel project. On your browser, go to http://localhost/phpmyadmin and create a new database with a nice name. In my case, I name my database as “management_system”.
7) Configure the database connections.
Now open the .env file again and change the values of each variable as follows.
The MySQL module coming with XAMPP package does not have a password by default. Therefore I have removed the DB_PASSWORD line completely in the above example. Also note that root is the default username.
In case you have changed the default ports in your XAMPP server, make sure you use the new port number for DB_PORT. (e.g:- DB_PORT=330633)
8) Commit changes to Git.
You can now start the development work with Laravel. First of all, add all changes to Git with the below commands,
git add .
git commit –m “Initial Commit”
git push origin master
Congratulations, you are now all set to get started with a fresh Laravel project! Happy coding folks!
Recommended Study Resources for Beginners
If you are a beginner and have no idea on where to start, you may just have a look on this video series. You have a lot to learn in Laravel, but this video series would make your life easy.
Laravel PHP - Create a Social Network (Full App) by Mindspace - View Playlist (22 Videos)