Laravel is the Most Starred PHP Framework on Github: more than 30 000 developers from all over the world (mostly from the USA) use it for custom software development. Thinking about Laravel 5 but don’t know where to start? Among the features that make Laravel framework better than others there are awesome documentation, community and step-by-step tutorials. In this post, to save your time, we've gathered expert advices on how to download, install and configure your first Laravel-based project.
#1 Installing Laravel Homestead Virtual Machine
Use Laravel Homestead (pre-packaged Vagrant box) as your development environment. Vagrant is the command line utility for building and maintaining portable virtual development environments.
Laravel Homestead runs on any Windows, Mac, or Linux system, and includes the Nginx web server, PHP 7.0, MySQL, Postgres, Redis, Memcached and a variety of other useful utilities. With Laravel Homestead it is not required to separately install PHP, a web server, and any other server software on your local machine.
- https://laracasts.com/series/laravel-5-fundamentals/episodes/2 (Video: Virtual Machines and Homestead)
#2 Installing Laravel from Composer
Laravel framework consists of many separate components that are downloaded and put together automatically into a framework through a special tool called “Composer” (a tool for dependency management in PHP). So, before using Laravel, make sure you have Composer installed on your machine.
- https://laracasts.com/series/laravel-5-fundamentals/episodes/1 (Video: Meet Composer)
- https://laracasts.com/series/laravel-5-from-scratch/episodes/1 (Video: Initial Setup)
#3 Using Artisan command-line interface in Laravel
Artisan is the command-line interface included with Laravel. Artisan offers many useful commands that can help you perform various tasks, such as generating migrations or publishing a package's assets. In addition to the built-in commands, you can extend Artisan with your own commands.
Artisan comes with Laravel by default. If your
php command works fine, then the only thing you need to do is to navigate to the project's root folder. The root folder is the parent folder of the
#4 Creating Routes and Loading Views WITHOUT Controllers in Laravel
The "hello world" for frameworks is to figure out how to process a given URI, and display the appropriate HTML on the page.
Routes define the endpoints of a web application, the final destinations of incoming requests. All Laravel routes are defined in the route files, which are located in the
routes directory. These files are automatically loaded by the framework. For most applications, you will begin by defining routes in your
Views are used to build up the resulting page that the user will see in their browser when they click on a link or submit a form. Typically a view is the HTML response returned by the application to the browser initiating the request. Views can be loaded directly from the routing file. Views are stored in the
resources/views directory. View files have a .php file extension or a .blade.php file extension if you use the Blade templating engine.
Using route closures (defining all of your request handling logic as closures in route files, without controllers) is very useful for small projects that don't require too much functionality. Closure based routes cannot be cached. To use route caching, you must convert any closure routes to controller classes.
- https://laracasts.com/series/laravel-5-from-scratch/episodes/2 (Video: Your First View and Route)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/4 (Video: Passing Data to Views)
#5 Creating Routes and Loading Views WITH Controllers in Laravel
For most projects you may wish to organize request handling logic using controllers. Controllers are PHP classes that contain application’s logic and are called from the application’s routing files. Controllers contain methods called “actions” that direct the execution flow of a web application by working with application’s data (models), producing output (views) or redirecting. Controllers can group related request handling logic into a single class. The controllers need to be connected to the routing mechanisms in order to be executed when the application runs. Controllers are stored in the
By creating a controller and specifying which routes in the routing file use its member functions you are able to take full advantage of MVC pattern in your Laravel applications.
Another important advantage of using controllers with routes instead of putting all code into the “route.php” file is that using separate PHP classes to build application’s functionality brings the full benefits of Object Oriented Programming (OOP) such as dependency injection, encapsulation, inheritance and more. This aspect of application architecture is incredibly important for developers who practice good design patterns in their applications.
If your application is using controller based routes, you should take advantage of Laravel's route cache. Using the route cache will drastically decrease the amount of time it takes to register all of your application's routes. In some cases, your route registration may even be up to 100x faster.
- https://laracasts.com/series/laravel-5-fundamentals/episodes/3 (Video: A Gentle Introduction to Routing, Controllers, and Views)
- https://laracasts.com/series/laravel-5-from-scratch/episodes/4 (Video: Routing to Controllers)
#6 Using Blade templating language to create templates in Laravel
Laravel uses its own view template engine called “Blade”. Blade template engine is fantastic for reducing or simplifying the code that you write in your view. Blade template engine also allows you to easily create layouts, extend them and include partials to prevent you from repeating the same HTML in multiple files. Using Blade makes it is possible to combine many templates (for example, header.blade.php, footer.blade.php and sidebar.blade.php) into a single HTML output. To reduce duplication, Blade provides "layout files." Within these files, you can define the wrapping HTML, which nests the content from each of your views.
- https://laracasts.com/series/laravel-5-from-scratch/episodes/3 (Video: View Data and Blade)
- https://laracasts.com/series/laravel-5-from-scratch/episodes/5 (Video: Layout Files)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/5 (Video: Blade)
#7 Adding/managing your CSS and JS files in Laravel
But if you need to use CSS preprocessors, or concatenate some files and minify them, Laravel Elixir (a tool to integrate Gulp into your Laravel projects, a task runner built as a wrapper around Gulp) will help you.
Elixir is built on top of Gulp, so to run your Laravel Elixir tasks you only need to run the
gulp command in your terminal. Make sure that you install Gulp globally first.
- https://laracasts.com/series/laravel-5-from-scratch/episodes/6 (Video: How to Manage Your CSS and JS)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/19 (Video: Manage Your Assets)
#8 Database Management and Database Operations in Laravel
Currently, Laravel supports four database systems: MySQL, Postgres, SQLite, SQL Server. The database configuration for your application is located at
config/database.php. In this file you may define all of your database connections, as well as specify which connection should be used by default.
Laravel interprets the data stored in the database into objects that you can easily manipulate like you would manipulate any other PHP object. This allows the developer to focus on other parts of the application instead of figuring out complex Structured Query Language (SQL) queries necessary to work with the data.
Laravel comes with two powerful sets of features to execute database operations, by using its own Query Builder or by using concept of “models” in Eloquent ORM. You can use both in the same application (even combine both to get the most flexibility out of DB operations).
Laravel’s Query Builder provides a clean and simple interface for executing database queries. It is a powerful tool that can be used for various database operations such as retrieving records, inserting new records, deleting records, updating records, performing “Join” queries, executing raw SQL queries, filtering, grouping and sorting of records
Laravel makes interacting with databases extremely simple using the Eloquent ORM (the most preferred way to fetch data and render it in the view). The Eloquent ORM provides an ActiveRecord implementation to work with your database. This means that each model corresponds to a table in your database. We can use Eloquent to interact with our database and do the basic CRUD functions, set up and use one-to-one relationships, one-to-many relationships, AND many-to-many relationships.
Some of the advanced database operations that Laravel provides out of the box are managing database tables and fields through special files called “migrations”, populating database with sample data (also called “seeding”), working with separate read/write databases, pagination, many different relationship types and advanced methods of querying relations, database transactions and query logging.
- https://laracasts.com/series/laravel-5-from-scratch/episodes/7 (Video: Fetching data)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/7 (Video: Migrations)
- https://laracasts.com/series/laravel-5-from-scratch/episodes/8 (Video: Defining Relationships With Eloquent)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/8 (Video: Eloquent)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/9 (Video: Basic Model/Controller/View Workflow)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/11 (Video: Dates, Mutators, and Scopes)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/27 (Video: Loose Ends and Wrapping Up)
- http://www.laravelsd.com (Laravel Schema Designer)
#8.1 Laravel CRUD operations
Forms exist in just about every single web application. How does it work in Laravel? Let's review the basic workflow for submitting a form, fetching the data that was sent through, and then creating a record in the database.
You'll of course need to modify existing records in the database. Let's review the process of creating an "edit" page to handle this very task. Along the way, we'll also get a chance to review PATCH requests and the concept of eager loading and the problems it solves.
Along the way, you'll be introduced to custom packages, service providers, and much more.
- https://laracasts.com/series/laravel-5-from-scratch/episodes/9 (Video: Forms)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/10 (Video: Forms)
- https://laracasts.com/series/laravel-5-from-scratch/episodes/10 (Video: Updating Records and Eager Loading)
#8.2 Validation in Laravel
Laravel provides several different approaches to validate your application's incoming data. Let's look at a complete example of validating a form and displaying the error messages back to the user.
- https://laracasts.com/series/laravel-5-from-scratch/episodes/11 (Video: Validation and More)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/12 (Video: Form Requests and Controller Validation)
- https://www.youtube.com/watch?v=UzoktoyzUM0 (Video: Add Validation Request Rules)
#8.3 Authentication and Authorisation in Laravel
Most applications will require some form of authentication. With Laravel, it is simple. Let's setup registration, authentication, and password resets in minutes.
- https://laracasts.com/series/laravel-5-from-scratch/episodes/13 (Video: Authenticate Your Users)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/15 (Video: Easy Auth)
#9 Using Laravel Middleware
Middleware provide a convenient mechanism for filtering HTTP requests entering your application. There are many more cases where you would like to use a middleware: changing the site language based on locale, enabling site-wide maintenance, sniffing bot traffic, logging etc. There are several middleware included in the Laravel framework, including middleware for authentication and CSRF protection. All of these middleware are located in the
app/Http/Middleware directory. Additional middleware can be written.
- https://laracasts.com/series/laravel-5-from-scratch/episodes/14 (Video: Understanding Middleware)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/16 (Video: Ogres Are Like Middleware)
#10 Laravel Flash Messages Example
Often, you'll want to send a quick notification to the user when they perform some kind of action in your application. "Good job, your task has been created." Or: "You are now logged out." So it seems that we need a way to store things in the session for just a single request. There are some clean ways to handle this in Laravel!
- https://laracasts.com/series/laravel-5-from-scratch/episodes/15 (Video: Flashing to the Session)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/20 (Video: Flash Messaging)
Laravel's service container (also known as the IoC container) is the heart of Laravel. It is a powerful tool for managing class dependencies and performing dependency injection.
Basically the IoC Container is just an ordinary PHP class. But we can place or “Bind” in it everything we need from interfaces implementations to directories paths and so on. Now, since we have a single Object that contains all of our various bindings, it is very easy to retrieve them back or “resolve” them at any point in our code.
- https://laracasts.com/series/laravel-5-from-scratch/episodes/16 (Video: Automatic Resolution and the Service Container)
- https://laracasts.com/series/laravel-5-fundamentals/episodes/26 (Video: The Service Container)
#12 Laravel Service Providers
Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services, are bootstrapped via service providers.
But, what do we mean by "bootstrapped"? In general, we mean registering things, including registering service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application. For example, when installing a package, you have to give application access to that package - one of the best solutions is through Service Providers list and a Facade.
Laravel ships with many facades which provide access to almost all of Laravel's features. In a Laravel application, a facade is a class that provides access to an object from the container.
Facades have many benefits. They provide a terse, memorable syntax that allows you to use Laravel's features without remembering long class names that must be injected or configured manually. Furthermore, because of their unique usage of PHP's dynamic methods, they are easy to test. However, some care must be taken when using facades.
- https://laracasts.com/series/laravel-5-from-scratch/episodes/17 (Bootstrapping With Service Providers)
Laravel Starter Kits
Videos are obviously useful without a doubt. However, many will agree that nothing compares to a working example of a functional code.
- https://www.youtube.com/watch?v=_dd4-HEPejU (Laravel 5.2 PHP Build a social network - Setup & Introduction)
- https://www.youtube.com/watch?v=R8B4og-BeCk&list=PLwAKR305CRO-Q90J---jXVzbOd4CDRbV (How to Build a Blog with Laravel)
Laravel Community are free discussion forums for asking and answering questions about using Laravel framework.
Twitter accounts to Follow
Do you need to read some pdf books about Laravel?
New to PHP?
Rate this article
Do you have a software development project to implement? We have people to work on it.
We will be glad to answer all your questions as well as estimate any project of yours.
Use the form below to describe the project and we will get in touch with you within 1 business day.