Skip to content

Instantly share code, notes, and snippets.

@m5lil
Created June 28, 2021 23:05
Show Gist options
  • Save m5lil/87c895fc5c0fc38cbcbe8251a3d9b075 to your computer and use it in GitHub Desktop.
Save m5lil/87c895fc5c0fc38cbcbe8251a3d9b075 to your computer and use it in GitHub Desktop.
[View composers] Set Meta Titles with View Composers #laravel #example
<?php
namespace App\Providers;
use App\Http\ViewComposers\MetaViewComposer;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
View::composer('admin.layouts.app', MetaViewComposer::class);
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
<?php
namespace App\Http\ViewComposers;
use Illuminate\Support\Facades\Request;
use Illuminate\View\View;
class MetaViewComposer
{
public function compose(View $view)
{
$view->with('metaTitle', ucfirst(Request::segment(count(Request::segments()))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment