{"id":1728,"date":"2021-06-20T12:03:15","date_gmt":"2021-06-20T09:03:15","guid":{"rendered":"https:\/\/www.etemkeskin.com\/?p=1728"},"modified":"2021-06-20T12:21:28","modified_gmt":"2021-06-20T09:21:28","slug":"creating-custom-laravel-blade-directive-in-laravel-8","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/20\/creating-custom-laravel-blade-directive-in-laravel-8\/","title":{"rendered":"Creating Custom Laravel Blade Directive in Laravel 8"},"content":{"rendered":"\n<p>Laravel, one of the most used web frameworks in the world and developed with PHP, comes with Blade template engine. The directives developed for specific purposes such as <strong><code>@section, @yield, @parent, @json<\/code><\/strong>, which come as predefined in Blade, provide great convenience to the user while developing the web application. But sometimes we need to write our own directive for our needs. In this post, I will explain how we can create our own directive in <strong>Laravel<\/strong>.<\/p>\n\n\n\n<p>First, we create our directive in the <strong><code>boot()<\/code><\/strong> method in the <strong>AppServiceProvider.php<\/strong> class in the Providers folder. As seen in the code block below, we have defined our own directive called <strong>my_method<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n\nnamespace App\\Providers;\n\nuse Illuminate\\Support\\ServiceProvider;\nuse Illuminate\\Support\\Facades\\Blade;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    \/**\n     * Register any application services.\n     *\n     * @return void\n     *\/\n    public function register()\n    {\n        \/\/\n    }\n\n    \/**\n     * Bootstrap any application services.\n     *\/\n    public function boot()\n    {\n        \/**My Custom Blade *\/\n        Blade::directive('my_method', function ($value = null) {\n            \n            $value = &quot;| &quot;.$value.&quot; |&quot;;\n\n            return &quot;&lt;h1 style='color: green'&gt; $value &lt;\/h1&gt;&quot;;\n        });\n    }\n}<\/pre><\/div>\n\n\n\n<h4>Usage<\/h4>\n\n\n\n<p>We can use the custom directive we created in blade files as follows.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;div class=&quot;container&quot;&gt;\n    &lt;div class=&quot;row justify-content-md-center&quot;&gt;\n\n        @my_method('this is my first directive.')\n  \n    &lt;\/div&gt;\n&lt;\/div&gt;<\/pre><\/div>\n\n\n\n<p>When we run the application file, we will get the following output in the internet browser.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"579\" height=\"141\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/06\/image-3.png\" alt=\"\" class=\"wp-image-1738\"\/><figcaption>Output of Directive On Browser<\/figcaption><\/figure><\/div>\n\n\n\n<p>Good luck &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel, one of the most used web frameworks in the world and developed with PHP, comes with Blade template engine. The directives developed for specific purposes such as @section, @yield, @parent, @json, which come as predefined in Blade, provide great convenience to the user while developing the web application. But sometimes we need to write [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating Custom Laravel Blade Directive in Laravel 8 - blog website<\/title>\n<meta name=\"description\" content=\"Laravel, one of the most used web frameworks in the world and developed with PHP, comes with Blade template engine. The directives developed for specific purposes such as @section, @yield, @parent, @json, which come as predefined in Blade, provide great convenience to the user while developing the web application. But sometimes we need to write our own directive for our needs. In this post, I will explain how we can create our own directive in Laravel.First, we create our directive in the boot() method in the AppServiceProvider.php class in the Providers folder. As seen in the code block below, we have defined our own directive called my_method.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/20\/creating-custom-laravel-blade-directive-in-laravel-8\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Custom Laravel Blade Directive in Laravel 8 - blog website\" \/>\n<meta property=\"og:description\" content=\"Laravel, one of the most used web frameworks in the world and developed with PHP, comes with Blade template engine. The directives developed for specific purposes such as @section, @yield, @parent, @json, which come as predefined in Blade, provide great convenience to the user while developing the web application. But sometimes we need to write our own directive for our needs. In this post, I will explain how we can create our own directive in Laravel.First, we create our directive in the boot() method in the AppServiceProvider.php class in the Providers folder. As seen in the code block below, we have defined our own directive called my_method.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/20\/creating-custom-laravel-blade-directive-in-laravel-8\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-20T09:03:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-20T09:21:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/06\/image-3.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.etemkeskin.com\/#website\",\"url\":\"https:\/\/www.etemkeskin.com\/\",\"name\":\"blog website\",\"description\":\"Etem KESK\\u0130N\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.etemkeskin.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"tr\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/20\/creating-custom-laravel-blade-directive-in-laravel-8\/#primaryimage\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/06\/image-3.png\",\"contentUrl\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/06\/image-3.png\",\"width\":579,\"height\":141},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/20\/creating-custom-laravel-blade-directive-in-laravel-8\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/20\/creating-custom-laravel-blade-directive-in-laravel-8\/\",\"name\":\"Creating Custom Laravel Blade Directive in Laravel 8 - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/20\/creating-custom-laravel-blade-directive-in-laravel-8\/#primaryimage\"},\"datePublished\":\"2021-06-20T09:03:15+00:00\",\"dateModified\":\"2021-06-20T09:21:28+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"description\":\"Laravel, one of the most used web frameworks in the world and developed with PHP, comes with Blade template engine. The directives developed for specific purposes such as @section, @yield, @parent, @json, which come as predefined in Blade, provide great convenience to the user while developing the web application. But sometimes we need to write our own directive for our needs. In this post, I will explain how we can create our own directive in Laravel.First, we create our directive in the boot() method in the AppServiceProvider.php class in the Providers folder. As seen in the code block below, we have defined our own directive called my_method.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/20\/creating-custom-laravel-blade-directive-in-laravel-8\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/20\/creating-custom-laravel-blade-directive-in-laravel-8\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/20\/creating-custom-laravel-blade-directive-in-laravel-8\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating Custom Laravel Blade Directive in Laravel 8\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\",\"name\":\"etemkeskin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/#personlogo\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"caption\":\"etemkeskin\"},\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/author\/etemkeskinyahoo-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/1728"}],"collection":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/comments?post=1728"}],"version-history":[{"count":24,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/1728\/revisions"}],"predecessor-version":[{"id":1758,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/1728\/revisions\/1758"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=1728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=1728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=1728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}