Skip to content

Instantly share code, notes, and snippets.

@tommymarshall
Created June 2, 2016 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommymarshall/f875c5e2cffac20a66184f259612f306 to your computer and use it in GitHub Desktop.
Save tommymarshall/f875c5e2cffac20a66184f259612f306 to your computer and use it in GitHub Desktop.
Feed meta data values into a view only once based on a route (right now, every time a view is called it gets overwritten) ie. Want to reference in my layouts.base {{ meta.title }}.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class MetaViewServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composer('*', function ($view) {
$view_name = $view->getName();
$meta = trans('meta.default');
if (array_key_exists($view_name, trans('meta')))
{
$meta = trans('meta')[$view_name];
}
return $view->with(compact('meta'));
});
}
// required method to satisfy abstract class method signature
public function register() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment