8. July 2019
von Blackbam

For understanding this Q&A style tutorial you should have basic knowledge about PHP programming and frameworks. You do not have to know Laravel yet. For the real documentation just learn https://laravel.com/docs/ – most information in this tutorial is just a real short summary of the documentation. It is useful though if you have to learn about what Laravel is and what the most important concepts and components are.

 

Why should I even use Laravel?

Because it is a super clean, easy to use PHP framework. My personal experience (this is subjective) told me that it is way easier to understand than the whole Symfony framework (Laravel is built on Symfony) or the Zend Frameworks. Symfony and Zend are both great frameworks, for sure, but Laravel offers you simplicity.

Laravel enables you to use best practice coding techniques for object-oriented programming. It comes with a powerful set of features for building solid web applications and assists you with solving common as well as complex problems.

 

Why shall Laravel be used with the newest PHP version (best will be the upcoming PHP 7.4)?

Laravel makes heavy use of modern features within the PHP language. It is completely object oriented and uses e.g. typed input parameters which came with PHP 7.

The real great thing about PHP 7.4 is that it offers typed properties, covariant returns (more generic in subclass) and contravariant input parameters (more specific in subclass). This is extremely powerful (you should read more about it)!

 

What is Dependency Injection?

Definition (Wikipedia): “In software engineering, dependency injection is a technique whereby one object supplies the dependencies of another object. A “dependency” is an object that can be used, for example a service. Instead of a client specifying which service it will use, something tells the client what service to use. The “injection” refers to the passing of a dependency (a service) into the object (a client) that would use it. The service is made part of the client’s state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.

The intent behind dependency injection is to achieve Separation of Concerns of construction and use of objects. This can increase readability and code reuse.

Dependency injection is one form of the broader technique of inversion of control. The client delegates the responsibility of providing its dependencies to external code (the injector). The client is not allowed to call the injector code; it is the injecting code that constructs the services and calls the client to inject them. This means the client code does not need to know about the injecting code, how to construct the services or even which actual services it is using; the client only needs to know about the intrinsic interfaces of the services because these define how the client may use the services. This separates the responsibilities of use and construction.”

That means that an object either is provided with another required object within its constructor or it just specifies some interface which is used for type hinting in constructors or methods.

The purpose of this is to get rid of dependencies and to achieve loose coupling (and high cohesion).

 

What is a Service Container and what is the Laravel Service Container doing?

A Service Container (or dependency injection container) is simply a PHP object that manages the instantiation of services (i.e. objects). The Service Container in Laravel is a Dependency Injection Container and a Registry for the application. The advantages of using a Service Container over creating manually your objects are.

It is often called with App::bind App:make etc.

The service provider is creating concrete classes automatically when required.

Class dependencies can be managed on object creation and it is used as a registry.

Laravel does this with binding certain services to the App:

$this->app->bind('FooService', \App\Services\FooService::class);

Essentially we are telling Laravel: “Hey store this object in your bag of tricks and label it as FooService”. Remember to always bind your services within the register method of your service providers.

There are also events in the service container where somebody can register to.

 

What is a Service Provider?

Core part of a Laravel application, resides in Providers directory. Has usually a special purpose like e.g. Authentication, Translation, ….

Service providers are usually registered on application bootstrap. All service providers are registered in app/config.php.

A service provider has a register and a boot method. The register method is called early and is for registering with the Service Container, the boot method is called after all Services has been registered and therefore you can use all Service Container within it.

 

What is „type hinting“?

Methods / functions with typed input parameters (optional in PHP).

 

What is (dynamic) binding in Laravel?

Laravel usually means binding a service provider to the service container. Almost all service container bindings will be registered within service providers.

 

What is middleware?

A layer between a client request from the frontend and a resource (data) in the backend. Therefore the logical part oft he backend which glues certain programs / part of a program together. It simplifies backend resource requests.

Middleware also serves the purpose of load balancing (distributing requests), concurrency, securring the backend,  …

Laravel offers many Middleware objects. One middleware for instance is for authentication, but you can also use middleware for certain headers for instance.

 

What is a Facade?

A Facade is a programming design pattern. It usually offers a simplified standardized interface to a certain set of sub-systems.

In Laravel: Facades provide a “static” interface to classes that are available in the application’s service container. Laravel ships with many facades which provide access to almost all of Laravel’s features. Laravel facades serve as “static proxies” to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.

Examples for Facades in Laravel are calls to App::, Auth::, DB::, Cookie:: …

Facades can be useful because they are not required in class constructors, but they can also make an application complicated to maintain in case they get to large.

 

What is a Contract in Laravel?

Laravel’s Contracts are a set of interfaces that define the core services provided by the framework. The Illuminate\Contracts\Queue\Queue contract i.e. defines the methods needed for queueing jobs, while the Illuminate\Contracts\Mail\Mailer contract defines the methods needed for sending e-mail.

Contracts are often imported with the use keyword and used (e.g. in the constructor or some method). It is easy to write alternative Contract implementations and therefore they can be replaced easily.

 

What is PHP artisan?

PHP artisan is command line tool that makes your life easier. In fact, very much easier while working with Laravel. If you know about any MVC framework, you would know what I’m about to say, it can create models, controllers, seeders, migrations and many other things with minimum (or maximum) boilerplate code. It can also start a server and can do many other things.

 

How is routing working in Laravel?

Usually defined in routes/web.php (for API routs for instance routes/api.php is used). Basically defined with the Route Facade which offers the common HTTP methods and a callback function. Routes have certain rules for parameters (also wildcard), can be grouped, …

 

What are Controllers in Laravel?

Handling all requests within route files can be clumsy. Controllers can do the most important calls to the model/business layer and return everything properly.

 

What is the Request/Response object doing?

The service container can automatically inject Request/Response objects (e.g. into a certain Controller). It contains info like e.g. input parameters, cookies, and does some sanitation on it.

Routes and Controllers usually should return response objects or views. You can add headers and cookies, create downloads, define the output, …

 

How are views in Laravel working?

In most projects Blade is used as a template engine. The views are built with html and contain output injected by Blade.

 

How is validation in Laravel working?

Laravel has a powerful set of validation rules which can be performed on inputs (e.g. in Controllers).

 

How is Laravel to databases?

Laravel usually uses the Eloquent ORM mapper. It simplifies database communication and SQL statements with certain methods. It offers a query builder which simplifies queries.

 

How are migrations and seeding working in Laravel?

Migrations are like version control for your database, allowing your team to easily modify and share the application’s database schema. Migrations are typically paired with Laravel’s schema builder to easily build your application’s database schema. If you have ever had to tell a teammate to manually add a column to their local database schema, you’ve faced the problem that database migrations solve.

The Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel’s supported database systems.

Laravel has a Seeder for seeding applications with defined or random data which is really helpful during development.

 

Be free to critizise this tutorial. Improvement ideas are always welcome! 😉 

Share

Warning: Undefined variable $time_since in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php on line 42 Dieser Eintrag wurde am 8. July 2019 um 21:21 in der Kategorie Frameworks, Laravel, PHP, Web Development veröffentlicht. You can book the comments for this article RSS 2.0. Feedback, discussion, commendation and critics are welcome: Write a comment or trackback.


Tags: , , , , , ,

Fatal error: Uncaught Error: Undefined constant "Ext_related_links" in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php:75 Stack trace: #0 /home/.sites/609/site1266/web/blackbams-blog/wp-includes/template-loader.php(106): include() #1 /home/.sites/609/site1266/web/blackbams-blog/wp-blog-header.php(19): require_once('/home/.sites/60...') #2 /home/.sites/609/site1266/web/blackbams-blog/index.php(17): require('/home/.sites/60...') #3 {main} thrown in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php on line 75 WordPress › Error

There has been a critical error on this website.

Learn more about troubleshooting WordPress.