1020 1065 1070 1895 1090 1318 1594 1582 1265 1749 1466 1776 1427 1362 1457 1658 1544 1829 1134 1803 1116 1046 1099 1960 1880 1074 1994 1293 1134 1574 1670 1084 1476 1310 1287 1122 1114 1310 1137 1724 1005 1159 1843 1923 1632 1269 1296 1878 1571 1074 1581 1645 1865 1348 1557 1226 1910 1533 1910 1878 1408 1833 1513 1556 1626 1885 1823 1386 1636 1130 1653 1947 2000 1208 1778 1276 1507 1655 1369 1482 1387 1713 1127 1659 1730 1926 1878 1611 1766 1534 1418 1525 1094 1324 1823 1661 1603 1149 1102 Statamic SEO Pro with Laravel Controllers | PHPnews.io

PHPnews.io

Statamic SEO Pro with Laravel Controllers

Written by Eric L. Barnes / Original link on Mar. 9, 2021

Statamic has a great first party plugin called SEO Pro that includes a lot of settings every site probably needs for search engine optimization including things like og tags, dynamic titles, descriptions, and even canonical urls.

It has been working perfectly for me but today I hit a small snag with how it tries to dynamically set the canonical url and it wasn’t working when loading Statamic Antlers views through a Laravel controller. For example, here is the code I use to show an about page:

return (new \Statamic\View\View)
    ->template('about')
    ->layout('layout')
    ->with([
        'content' => 'my content here',
    ]);

When this page is loaded the SEO Pro plugin was using the home page as the canonical url.

<link href="https://mysite.com" rel="canonical" />

To set your own canonical URL you just need to pass it manually like this:

return (new \Statamic\View\View)
    ->template('about')
    ->layout('layout')
    ->with([
        'content' => 'my content here',
        'seo' => ['canonical_url' => 'https://mysite.com/about']
    ]);

With that in place the plugin will pull the setting you pass and use it in the parsed html source. I’m sure you can also change other settings this way as well, so if you ever need to dynamically customize what data it uses look at adding to an “seo” array when passing your data to the view.

ericlbarnes ericlbarnes ericlbarnes

« A Tool that helps you to Migrate to ECS - Xdebug Update: February 2021 »