When it comes to building large and complex web applications you want everything to be fast and secure, with Laravel, a MVC php framework, you can get an expressive and elegant syntax while taking the pain out of development by easing common tasks used in the majority of web applications, such as:

  • Simple, fast routing engine.
  • Powerful dependency injection container.
  • Multiple back-ends for session and cache storage.
  • Database agnostic schema migrations.
  • Robust background job processing.
  • Real-time event broadcasting.

Every task can be performed using a php CLI called Artisan, for example you can start a new local development environment and run the application on the browser with php artisan serve launched on the root folder where the script is located.

Let’s take a simple blog application as an example of how it can be structured:

AUTHENTICATION

Simple Authentication is something that Laravel have out of the box, it’s just a command:

php artisan make:auth

This command should be used on fresh applications and will install a layout view, registration and login views, as well as routes for all authentication end-points.

MODEL

Blog applications basically contain blog posts so we’ll start with the Post model. They’re typically stored in the app/Http directory.

Here’s an example:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
    protected $guarded = [];
    /**
     * The belongs To Relationship
     *
     * @var array
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }     
}

In the Model you declare methods and database relationships between other class objects like the “Belongs TO” relationship with a User.

CONTROLLER

The controller is a class that contains and handles all the logic for every route of the app. They’re typically stored in the app/Http/Controllers directory.

Here’s an example:

<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
class PostsController extends Controller
{
 use AuthorizesRequests;
 public function __construct()
 {
  // Require the user to be logged in
  $this->middleware('auth', ['except' => ['show']]);
 }
  private function getPostByAlias($alias) {
  return Post::where('alias', $alias)->firstOrFail();
 }
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
  $user = auth()->user();
  $posts= Post::where('user_id', '=' , $user->id)->latest()->get();
  return view('posts.index', compact('posts'));
 }

In this Controller you are handling the logic for the index route, which usually is the front page with a end-point of “/” and in this case you are returning all the latest posts of the logged in user.

VIEWS FOR THE FRONTEND

Views in Larabel are chunks of code connected together through a templating engine called Blade.

Unlike other popular PHP templating engines, Blade does not restrict us from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.

Here’s an example:

<!-- Stored in resources/views/layouts/app.blade.php -->
<html>
 <head>
   <title>{{ config('app.name', 'Laravel') }} - @yield('title')</title>
 </head>
 <body>
   @section('sidebar')
    This is the master sidebar.
   @show
   <div class="container">
    @yield('content')
   </div>
 </body>
</html>

This simple view is meant to be a parent that outputs other children's code in the content section with @yield(‘content’).

FRONTEND SCAFFOLDING

While Laravel does not dictate which JavaScript or CSS pre-processors you can use, it does provide a basic starting point using Bootstrap, React, and / or Vue that will be helpful for many applications.

To start using frontend scaffolding you have to install a package using Composer:

composer require laravel/ui --dev

Once the laravel/ui package has been installed, you can install the frontend scaffolding that you prefer using the ui Artisan command:

// Generate basic scaffolding...
php artisan ui bootstrap
php artisan ui vue
php artisan ui react
Hope you find Laravel interesting, to learn more about what you can do in-depth, you can visit the Laravel Docs.
Share