Skip to content

Instantly share code, notes, and snippets.

@vanchelo
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save vanchelo/08a93ca7a04cd05949ff to your computer and use it in GitHub Desktop.

Select an option

Save vanchelo/08a93ca7a04cd05949ff to your computer and use it in GitHub Desktop.
<?php namespace App\Http\Controllers;
use App\Classes\Title;
class MyController extends Controller
{
/**
* @var Title
*/
protected $title;
function __construct(Title $title)
{
$this->title = $title;
}
public function index()
{
$this->title->prepend('Главная страница');
}
}
class MyController extends Controller
{
/**
* @var Title
*/
protected $title;
function __construct()
{
$this->title = app('title');
}
public function index()
{
$this->title->prepend('Главная страница');
}
}
class MyController extends Controller
{
public function index(Title $title)
{
$title->prepend('Главная страница');
}
}
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Classes\Title;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('App\Classes\Title', function ()
{
// Здесь можно из конфига значение подставить
return new Title('Название сайта');
});
$this->app->bind('title', 'App\Classes\Title');
}
}
<title>{{ app('title') }}</title>
<?php
class Title
{
protected $values = [];
protected $delimiter;
function __construct($value, $delimiter = ' :: ')
{
$this->values[] = $value;
$this->delimiter = $delimiter;
}
public function append($value = '', $delimiter = null)
{
$this->values[] = $delimiter ?: $this->delimiter;
$this->values[] = $value;
}
public function prepend($value = '', $delimiter = null)
{
array_unshift($this->values, $value, $delimiter ?: $this->delimiter);
}
public function render()
{
return implode('', $this->values);
}
public function last()
{
return end($this->values);
}
public function first()
{
return reset($this->values);
}
function __toString()
{
return $this->render();
}
}
@vanchelo
Copy link
Copy Markdown
Author

vanchelo commented Dec 1, 2014

How to Use?

$title = new Title('Site name', ' - ');
$title->prepend('Category');
$title->prepend('Article');

Default values delimiter is ::
And in View Layer

<title><?= $title ?></title>

This will render

<title>Article - Category - Site name</title>

@vanchelo
Copy link
Copy Markdown
Author

vanchelo commented Dec 1, 2014

Как использовать?

$title = new Title('Название сайта', ' - ');
$title->prepend('Категория');
$title->prepend('Статья');

По умолчанию delimiter (разделитель значений) ::

И пишем в шаблоне

<title><?= $title ?></title>

На выходе получаем такой код

<title>Статья - Категория - Название сайта</title>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment