Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Create Custom Directives in Laravel

How to create custom directive in Laravel 8 ?

Aman Jain, November 11, 2021November 5, 2023

Laravel blade is powerful template engine which makes short tags directive and easy to reuse templates. Most of the templates restrict to use other language but blade gives more flexibility and we can use PHP as well in template. Directives start with prefix @ and accepts parameter. In this article i will take a simple example to demonstrate the Laravel blade directive.

To create the custom directive Laravel provides Blade::directive method which accepts two parameters, one is name of directive and second callback function when its called from view.

Let’s begin with simple steps:

Step 1: Create a laravel project

First step is to create the Laravel 8 project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations

composer create-project laravel/laravel example-app

Step 2: Register Directive in AppServiceProvider

Next step is to register the directive in AppServiceProvider, In Laravel service providers are central place for bootstrapping any component, module, directive or itself Laravel. therefore, let’s register our directive in AppServiceProvider. In this example i will create a simple directive to show the long text in trimmed text format with dot.

<?php

namespace App\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        
        Blade::directive('ellipsis', function ($expression) {
            return "<?php strlen($expression)>100?substr($expression,0,100).'...':$expression; ?>";
        });
    }
}

Step 3: Create a view

Now, create a view in resources/views folder to check our implementation


 
        <div class="row">
         Long : {{$longText}} <br><br>
         
        Trimmed Text :  @ellipsis($longText)
        
        </div>
        
    <!-- credits -->
    <div class="text-center">
      <p>
        <a href="#" target="_top">Laravel 8 custom directive </a>
      </p>
      <p>
        <a href="https://readerstacks.com" target="_top">readerstacks.com</a>
      </p>
    </div>
   

Step 4: Create route to check implementation

Create a simple route and call the view

<?php

use Illuminate\Support\Facades\Route;
Route::get('/check-directive',function(){
        return view("custom_directive",['longText'=>'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum']);
});

Screenshot:

Screenshot 2021 11 11 at 9.20.02 AM
Laravel custom directive

Related

Php Laravel directivelaravelphptrimmed text

Post navigation

Previous post
Next post

Related Posts

Php Creating resource controller in laravel

How to create resource controller in Laravel 8 ?

November 7, 2021November 7, 2021

Resource controller is helpful when you want Create, Read, Update and Delete (CRUD) operations. If you have a model with same set of actions like crud then resource controller are useful. we can create resource controller easily using the artisan command. Above command will generate controller ImageController in \app\Http\Controllers. As…

Read More
Php How to create multiple size thumbs from image in laravel

How to create multiple size thumbs from image in laravel 9 ?

May 14, 2022February 22, 2024

Resizing images can be a best performance booster for any application since this will load the proper size images. In this article i will show you to create multiple size thumbs from image in laravel using intervention package. Big size of image can reduce the performance of the application therefor…

Read More
Php How to Use Bootstrap 5 in Laravel 9

How to Use Bootstrap 5 in Laravel 9 ?

May 30, 2022May 30, 2022

Bootstrap is a UI framework to enhance the beauty of website. In this tutorial i will show you to use Bootstrap 5 in laravel. Using the bootstrap we can enable multiple UI multiple functionality like we can show amazing buttons , card, input etc. Even with JavaScript library of bootstrap…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version