D
D
Dmitry2016-05-30 19:49:58
Laravel
Dmitry, 2016-05-30 19:49:58

Laravel 5 how to output meta tags correctly?

There is a main template in which there is a place for displaying meta tags and the page title.
Each of the controllers (for any page of the site, be it news, articles, etc.) receives the necessary information through the model, connects the necessary view and displays this information - everything is simple. If, say, we went inside the news, then the Show method of the controller passes all the necessary information about the news to the view through the $news variable. Including meta tags and title. Those. $news->meta...
But, let's say, a situation where the news does not have meta tags set, in this case, you need to display the default meta tags. Of course, you can do this inside the news controller, but there are many sections of the site, you won’t write default meta tags in each controller. Therefore, the question arises: how to organize the output of meta tags in such a way that somewhere in one place they would be assigned default values, and then controllers, if there are meta tags for the information displayed on the page, would replace these meta tags by default with the actual ones.
And ideally, do it like this: create a separate controller that would be responsible for generating meta tags, regardless of which section we are in. And the template would display these meta tags like this { meta::showMetaTags }
Those. this controller would receive information about the current section, take the necessary data from the database and output it to the template.
Anyone suggest how to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
D', 2016-05-30
@Denormalization

https://laravel.com/docs/5.2/views#view-composers
or view()->share()
We read, select.

T
Tesla, 2016-05-31
@Tesla

I just write in layout.blade <title>@yield('title', 'Default value')</title>
If the child template does not set the 'title' section, then 'Default value' is displayed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question