Skip to content
Home » Blog » What is Laravel?

What is Laravel?

  • by

Laravel: More elegance when programming

Laravel is a typical open source PHP framework that follows the MVC architecture pattern. Since the release of the first version in June 2011, the popularity has increased steadily.

Now with version 6.x, which has been available since September 2019, Laravel is the best open source PHP framework.

This version was also accompanied by the changeover to a Composer-based installation and package management.

The development of Laravel has always been significantly driven and shaped by Taylor Otwell, the initiator and maintan of the project. While there were often minor downward changes even in minor releases, the situation has now relaxed considerably. The Laravel documentation includes a detailed upgrade guide for each release with a list of the relevant changes and possible stumbling blocks. In addition, a version with long-term support was published for the first time in June 2015. LTS versions of Laravel are provided with bug fixes for two years after their release and then for a further year with security updates.

As a result, it’s a good time to consider using Laravel as a framework for new or existing projects. Apart from the important long-term support, it is above all the acceptance in the professional environment that speaks for an assignment. Well-known users of Laravel include the bargain portal mydealz and the online shop AboutYou.

MVC 101
As a full-stack framework, Laravel offers all the functions you need to develop professional web applications. Using Composer, a new project based on Laravel can be quickly set up with the following line: composer create-project –prefer-dist laravel / laravel mein-shop.

In its basic functions, Laravel has many parallels with other frameworks such as Symfony. The individual pages or httpssss endpoints of a web application are summarized in controller classes according to the MVC design pattern. The URLs for accessing it are defined as so-called routes. Here you are not restricted and you can freely design the URL scheme. Placeholders used can optionally be checked for a regular expression or used to automatically load a required model from the database and make it available in the controller:

 // routes.php
 Route::get(“orders/{order}/invoice.pdf”, “OrdersController@getInvoicePdf”);  
// OrdersController.php
 public function getInvoicePdf(Order $order) {
 return response()->download($order->getInvoicePath(), “invoice.pdf“);
 // Das Model mit der im Platzhalter genutzten ID ist direkt in der Controller-Methode verfügbar
 }

Routes can also be defined specifically for individual or all subdomains. This is particularly practical for SaaS applications in which each client has its own subdomain. The segment of the subdomain captured by a placeholder can be passed on as a parameter to the controller methods and used there.

Laravel supports the databases MySQL, PostgreSQL, SQLite, SQL Server and the key value store Redis. For working with a database, Laravel also offers an ORM called Eloquent, in addition to a query builder that simplifies the creation of secure SQL queries that are protected from SQL injections. Unlike Doctrine, for example, Eloquent does not use a data mapper pattern, but the active record pattern. While such an uncomplicated entry into development is possible, one should be aware that Active Record is an anti-pattern, at least with regard to the SOLID principles. Different responsibilities – especially domain / business logic and persistence – are mixed in one class, which often leads to so-called god objects.

Accordingly, the use of Eloquent is a question of taste and can be problematic, especially in complex projects. While Laravel is adapted to the use of Eloquent by default, there is no reason not to use another ORM instead, or to create the database queries directly using Query Builder and encapsulate them in repository classes. Aside from pure database access, Laravel offers with the migration classes an easily accessible function for creating and modifying the database schema. Realistic test data are required especially for automated testing and during development. The generation of test data can be mapped in separate seeder classes so that they do not have to be created manually every time or passed on in the form of SQL dumps. Various useful auxiliary functions are available here.
Laravel has its own template language for the output of HTML code. The similarities between Blade, the name, and the Razor template system from the ASP.NET framework are not accidental. The idea for Laravel came about when Taylor Otwell couldn’t find a framework for PHP comparable to ASP.NET. At its core, blade templates are ordinary HTML documents that are enhanced with logic by special tags. Laravel offers a whole range of them that make everyday developer work more comfortable:

@forelse($orders as $order)
  <li>{{ $order->id }}: {{ $order->total }} €</li>
@empty
  <p>Keine Bestellungen vorhanden.</p>
@endforelse
// Nützlich: Das Handling einer leeren Ergebnisliste wird durch Blade vereinfacht

Also part of Laravel is a frontend framework based on Vue.js and Bootstrap. If you don’t like Vue.js, you can switch the template to React using Artisan. Artisan is Laravel’s own command line tool, with which many tasks such as B. Code generation can be done. To simplify the configuration of webpack, the npm package Laravel Mix is ​​included. Standard tasks such as compiling and miniaturizing assets can be set up quickly and easily adapted.

For pure API projects, e.g. For example, in conjunction with a single page application, JWT-based authentication is offered. This is also preconfigured in the front end framework. In addition to the complete Laravel installation, Laravel Lumen is also a kind of “Laravel light” for the development of APIs. To improve performance, some functions that are less relevant for APIs have been removed. Among other things, the entire session handling is missing, which means that authentication must always take place using an API key or token. If the requirements of a project change, an upgrade to the full Laravel version is possible at any time.

Why laravel?
Rightly, the question arises, what exactly sets Laravel apart from the multitude of competing PHP frameworks and makes it a good choice. Since the functions mentioned so far or in a similar form are contained in almost every framework, I will go into a selection of features that stand out in my view.

The software architecture on which Laravel is based is very elegant. Dependency Injection is used throughout as a design pattern to increase the maintainability and testability of the applications. The linchpin here is the service container, which is responsible for the resolution and provision of dependencies. It is therefore not necessary to use global objects or static classes. Instead of manually instantiating the dependencies in the code, these are simply requested from the service container. This either happens explicitly by calling an auxiliary function or implies within objects that are also provided by the service container. This also includes all controller classes. If the parameter list of an object constructor contains a typed dependency, the service container will try to inject it into the constructor:

CartController extends Controller {
  public function __construct(CartService $cartService) {
    $this->cartService = $cartService;
  }
}
// die Abhängigkeit zum CartService wird automatisch aufgelöst, da der Cart Controller ebenfalls vom Service Container instanziert wird

By default, instantiation is simply done using new Dependency () ;. Service providers are available for complex objects with their own dependencies. These are separate classes that define how certain objects are to be created by the service container.

It is also practical that not only regular classes, but also abstract classes or interfaces can be specified as dependencies. To be able to resolve them, the registration of a specific implementation in the service provider is necessary. If you do not want to always have certain objects created again, they can also be defined as singleton. When this dependency is accessed for the first time, it is generated by the service container and this instance is always reused with every subsequent request.

In order to encapsulate functionality that affects more than one HTTP endpoint and is actually not part of the respective logic, a so-called middleware can be created. This is a class or function that encloses the actual controller method and can modify the request or response. Several middlewares can be connected in series, so that with an API, for example, authentication or throttling can be implemented in separate classes. In Laravel itself, session handling and CSRF token validation are implemented as middleware.

As already mentioned, Laravel comes with its own CLI tool called Artisan. Not only can this be used to carry out framework-specific tasks such as generating controller or model classes, but you can also implement your own functions (commands). These are typically the functions that you would like to run periodically as a cron job to create backups, send payment reminders or synchronize data with third-party systems. You can configure your own Artisan commands as a normal cron job, but it is easier to use the Task Scheduler. The interval can be defined using an easily understandable syntax, and it is only necessary to save the task scheduler itself as a cron job:

$scheduler->command(“meinshop:send-payment-reminder”)->daily();
// Das eigene Artisan Command wird täglich ausgeführt
Tags: