4

i'm tryng to integrate metatags in my layout laravel,

app.layouts

<title>SiRegala.it - @yield('title')</title>
<meta name="description" content="@yield('description')"/>
<link rel="canonical" href="@yield('canonical')"/>

view

@section('title')
Homepage
@stop
@section('canonical') 
<?php echoURL::current(); ?>
@stop

i'm tryng to get current url of my view, but actually i get this error:

Class 'echoURL' not found

How can i get Current URL ? maybe with blade? i tryed to search some solution with blade but i did not find nothing.

Thank you for your help!

Diego Cespedes
  • 1,353
  • 4
  • 26
  • 47

9 Answers9

11

Laravel 5.7

<link rel="canonical" href="{{ url()->current() }}" />
Farid Movsumov
  • 12,350
  • 8
  • 71
  • 97
7

You forgot to put space between echo and URL facade:

<?php echo URL::current(); ?>

Also, in Blade you usually want to avoid using <?php ?>:

{{ URL::current() }}
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
3

The purpose of canonical URLs is to avoid duplication of content, to start atleast the protocol and domain has to be specified

  <link rel="canonical" href="{{'https://example.com/' . request()->path()}}" />
Rejneesh
  • 1,574
  • 16
  • 16
  • 2
    I thought the same thing when looking through other answers, I believe at least the domain and protocol should be fixed. But I pull that from env `{{ config('app.url') . '/' . request()->path() }}` – Alan Rezende Jan 31 '22 at 21:44
  • Following SEO guidelines this should be the best approach using @AlanRezende extra config. Other solution will repeat content at several urls in case of http or https or www subdomain use – skyllet May 26 '22 at 18:06
2

Is another solution if you use this code:

put this in app layout, between <head></head> section.

<link rel="canonical" href="{{ url(Request::url()) }}" />

and you get current URL address

Johnny
  • 21
  • 1
0

use following code instead

{{ URL::current() }}

"avoid using of tags"

Kundan roy
  • 3,082
  • 3
  • 18
  • 22
0

canonical Must include the full path, use:

request()->fullUrl()

you can use this code in blade, like this:

<link rel="canonical" href="{!! request()->fullUrl() !!}" />

Not like this (because of htmlspecialchars problems ...):

<link rel="canonical" href="{{ request()->fullUrl() }}" />
0

It depends on what you are trying to achieve with your canonical statements.

The question implies the generation of self-referential canonical tags - i.e. the canonical tag refers exactly to the page being served.

This is good practice for the canonical pages (for the pages intended to be the 'master' pages) - but the desire to use canonical tags suggests there may be duplicate pages on the site. These 'duplicate' pages should be tagged to refer to the master or primary page not themselves.

i.e. the question only addresses part of the likely problem.

I run 2 sites with essentially the same content but under different branding for different organisations - so I have duplicate pages across different domains

Rejneesh's suggestion above works for me as it can be set up to redirect from one domain to another without having to hard code the links for each page.

<link rel="canonical" href="{{'https://example.com/' . request()->path()}}" />

For other sorts of duplication you may (will) need to find a more complex process to automatically link to the canonical page.

Andy
  • 11
  • 3
0

<link rel="canonical" href="{{ url()->current() }}" />

As answered by Farid works perfectly, simply add to your main layout file i.e guest.blade.php

Tested and implemented on Laravel 9 & 10 (live public sites)

Chris T
  • 1
  • 2
0

kind of late, but I've been using this approach and I think is SEO compliant, also with the cases about paginated things, etc.

<link rel="canonical" href="{{ Request::url() }}">
Francisco Daniel
  • 989
  • 10
  • 13