Answer the question
In order to leave comments, you need to log in
How to properly pass headers from controller/template to main template (laravel5)?
Hello.
I am studying Laravel 5 and it is not clear how to implement the following correctly:
There is a class that is responsible, say, for the h1 header and the title tag.
I want to call it from any controller or blade template. Something like Titles->set("Hello!"); Inside this method, I process the data and already in the main template I do Titles->getTitle() ; Titles->getH1();
Tell me in which direction to dig in order to correctly implement such functionality. I got completely lost in providers, service controllers, facades, etc...
Thanks in advance for your help!
Answer the question
In order to leave comments, you need to log in
Create a main helper class for this purpose, let's say app/Helpers/Titles.php :
<?php
namespace App\Helpers;
class Titles
{
private $title = null;
private $h1 = null;
public function setTitle($title) {
$this->title = $title;
}
public function getTitle() {
return $this->title;
}
public function setH1($h1) {
$this->h1 = $h1;
}
public function getH1() {
return $this->h1;
}
}
...
public function boot()
{
...
$this->app->singleton('Titles', function($app) {
return new \App\Helpers\Titles();
});
...
}
...
app()['Titles']->setTitle('Очередной интернет-магазин');
app()['Titles']->getTitle();
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Titles extends Facade
{
protected static function getFacadeAccessor() {
return 'Titles';
}
}
...
'aliases' => [
...
'Titles' => \App\Facades\Titles::class,
...
],
...
<?php
namespace App\Http\Controllers;
use Titles;
class UserController extends Controller
{
public function signup() {
Titles::setTitle('Добро пожаловать!');
return view('user.signup');
}
}
Main Template
I Page Template@section('title') SomeTitle @endsection
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question