39

What Is Laravel and Why Should You Care?

 5 years ago
source link: https://www.tuicool.com/articles/hit/2Ib2emz
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Laravel is a PHP framework that will drastically improve your productivity. It has been around for quite some time. The first version was released in June of 2011. However, the founder, Taylor Otwell, and his crew are actively improving it.

The latest version (5.7), only came out in September of 2018. It runs on PHP 7.1.3 and above. It follows the MVC (Model-View-Controller) pattern which allows for a much-needed separation of concerns in our web applications.

I will describe a few of the most important features present in Laravel and hopefully, I will convince you to give it a try. There's nothing to lose (except maybe one of your evenings filled with Netflix) and so much to be gained.

Features

Laravel’s most important features (in my opinion) are as follows.

Eloquent ORM (Object-Relational Mapping)

PHP’s implementation of the active record pattern . In simple terms, this pattern allows us to present database tables as classes. Instances of this class are tied to each row in the table. A bit confused? All this means is that you can define a class, e.g.  User , which will be tied to the “users” table in your database. Each row in the “users” table is then represented as an instance of the  User   class. Simple.

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Then to find a user in the database you can simply call User::find(1); where 1 is the unique id of the row in the users table. Eloquent obviously allows for more complex select queries.

Views With Blade

Views contain all of the HTML “code” served by your application. That way you don’t ever have to combine the business logic with the presentation layer. Views support Blade templating engine which allows you to write reusable pieces of HTML code as well as generic layout files. It becomes very useful when certain pages of your application consist of the same components, e.g. a footer. Here’s an example, as today we’re all about examples:

@extends('layouts.app')

@section('title', 'Page Title')

@section('sidebar')
    @parent

    <p>This is appended to the master sidebar.</p>
@endsection

@section('content')
    <p>This is my body content.</p>
@endsection

It's important to note that Blade templates must be saved with a .blade.php extension.

Controllers

Controllers allow for the grouping of related request handling logic into a single class.

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return View
     */
    public function show($id)
    {
        return view('user.profile', ['user' => User::findOrFail($id)]);
    }
}

Routing

A very simple routing mechanism with all the routes conveniently listed in one file is present. You can naturally have multiple files if you wish, especially if you have a bigger application that supports an API. Here’s an example of a route definition.

Route::get('profiles', ‘UserController@getProfiles');

Then you can use this route anywhere in any of your HTML forms (using Blade).

<form method="GET" action="/profiles">
    @csrf
    ...
</form>

Upon submission of this form, the routing mechanism will recognize the route and execute the code specified in the definition. In this case, Laravel will look for the UserController   class and the  getProfiles method.

As a bonusl, by adding the @csrf   annotation to the form, you protect your system from Cross-Site Request Forgery (CSRF) attacks.

Simple Authentication

Have you implemented a complete authentication mechanism with registration and login and then forgotten the password functionality in 10 seconds? No problem. Just run those two simple commands:

  • php artisan make:auth

  • php artisan migrate  

Most web applications must implement an authentication system but why reinvent the wheel? With Laravel it comes for free.

Conclusion

There are many benefits to implementing web applications with Laravel but first and foremost it will save you a lot of time.

The key features I've highlighted today are as follows:

  • Eloquent ORM

  • Blade Templating

  • Controllers

  • Nice Routing

  • Super quick authentication mechanism.

But there's so much more!

A huge amount of resources for beginners and a kick-ass documentation is also at your disposal. An amazing community of developers should also be mentioned. You can get started with Laravel and find out more here .

Happy coding!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK