A Package to Help set up SEO in Your Laravel Project

News

June 10th, 2019

A Package to Help set up SEO in Your Laravel Project

Easily set up SEO in your Laravel applications with the code4mk/lara-head package by Mostafa Kamal. Here’s an example of how you’d use this package to set Facebook Opengraph tags:

Khead::setOg([
"app_id" => [
"property"=>"fb:app_id",
"content"=>"123456789"
],
"url" => [
"property"=>"og:url",
"content"=>"https://example.com/page.html"
],
"type" => [
"property"=>"og:type", "content"=>"website"
],
"title" => [
"property"=>"og:title",
"content"=>"Content Title"
],
// ...
]);

Then in your blade template you’d access to Opengraph settings and render them out with the following:

{!! Khead::getOg() !!}

Here’s another example of setting the viewport (i.e., from a controller) and then rendering it on the frontend:

Khead::setMeta('viewport', [
"name" => "viewport",
"content"=>"width=device-width, initial-scale=1"
]);
 
// Render
{!! Khead::getMeta('viewport') !!}

To learn more about the full capabilities of this package, including source code, documentation, and examples, check out the project on GitHub at code4mk/lara-head.

I want to point out that this package is demonstrating using Blade’s unescape syntax. You should verify the underlying code you are using any time you intentionally don’t escape HTML—especially third-party code. Future upgrades are worth reevaluation to make sure the packages you use haven’t introduced any unintentional (or intentional) vulnerabilities.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.