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:
1Khead::setOg([ 2 "app_id" => [ 3 "property"=>"fb:app_id", 4 "content"=>"123456789" 5 ], 6 "url" => [ 7 "property"=>"og:url", 8 "content"=>"https://example.com/page.html" 9 ],10 "type" => [11 "property"=>"og:type", "content"=>"website"12 ],13 "title" => [14 "property"=>"og:title",15 "content"=>"Content Title"16 ],17 // ...18]);
Then in your blade template you’d access to Opengraph settings and render them out with the following:
1{!! Khead::getOg() !!}
Here’s another example of setting the viewport (i.e., from a controller) and then rendering it on the frontend:
1Khead::setMeta('viewport', [2 "name" => "viewport",3 "content"=>"width=device-width, initial-scale=1"4]);56// Render7{!! 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:
Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.