diff --git a/app/Models/Setting.php b/app/Models/Setting.php index 85dc901..93aceb1 100644 --- a/app/Models/Setting.php +++ b/app/Models/Setting.php @@ -14,4 +14,27 @@ class Setting extends Model * @var array */ protected $fillable = ['key', 'value', 'tag']; + + /** + * @param \Illuminate\Database\Eloquent\Builder $query + * @param string $tag + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeTag($query, $tag = null) + { + if (is_null($tag)) { + return $query; + } + + return $query->where('tag', $tag); + } + + /** + * @param null $tag + * @return mixed + */ + public function formatData($tag = null) + { + return $this->tag($tag)->pluck('value', 'key')->toArray(); + } } diff --git a/app/Observers/SettingObserver.php b/app/Observers/SettingObserver.php new file mode 100644 index 0000000..6c7f399 --- /dev/null +++ b/app/Observers/SettingObserver.php @@ -0,0 +1,21 @@ +cacheHelper->keySiteSettings()); + } +} \ No newline at end of file diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 9c28f9d..716ee08 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -4,10 +4,15 @@ use App\Models\Category; use App\Models\Post; +use App\Models\Setting; use App\Models\Tag; use App\Observers\CategoryObserver; use App\Observers\PostObserver; +use App\Observers\SettingObserver; use App\Observers\TagObserver; +use App\Repositories\Contracts\SettingRepository; +use App\Services\CacheHelper; +use Illuminate\Contracts\Container\Container; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\ServiceProvider; @@ -27,6 +32,13 @@ public function boot() Post::observe(PostObserver::class); Tag::observe(TagObserver::class); Category::observe(CategoryObserver::class); + Setting::observe(SettingObserver::class); + + $this->app->singleton('settings', function (Container $app) { + return $app['cache']->rememberForever(CacheHelper::keySiteSettings(), function () use ($app) { + return $app->make(SettingRepository::class)->siteSettings(); + }); + }); } /** diff --git a/app/Providers/ComposerServiceProvider.php b/app/Providers/ComposerServiceProvider.php index 63930ac..c517882 100644 --- a/app/Providers/ComposerServiceProvider.php +++ b/app/Providers/ComposerServiceProvider.php @@ -26,12 +26,6 @@ public function boot() View::composer('widgets.tag', TagsComposer::class); View::composer('widgets.hot', HotPostsComposer::class); View::composer('partials.comment', CommentComposer::class); - View::composer('layouts.app', function (\Illuminate\View\View $view) { - $view->with([ - 'keywords' => '', - 'description' => '' - ]); - }); } /** diff --git a/app/Repositories/Contracts/SettingRepository.php b/app/Repositories/Contracts/SettingRepository.php index effbf7d..4bb5eaa 100644 --- a/app/Repositories/Contracts/SettingRepository.php +++ b/app/Repositories/Contracts/SettingRepository.php @@ -8,5 +8,9 @@ */ interface SettingRepository extends RepositoryInterface { - + /** + * @param null $tag + * @return mixed + */ + public function siteSettings($tag = null); } diff --git a/app/Repositories/Eloquent/SettingRepositoryEloquent.php b/app/Repositories/Eloquent/SettingRepositoryEloquent.php index a767fd7..b0591f1 100644 --- a/app/Repositories/Eloquent/SettingRepositoryEloquent.php +++ b/app/Repositories/Eloquent/SettingRepositoryEloquent.php @@ -18,4 +18,17 @@ public function model() { return Setting::class; } + + /** + * @param null $tag + * @return array|mixed + */ + public function siteSettings($tag = null) + { + if (method_exists($this->model, $method = 'formatData')) { + return call_user_func_array([$this->model, $method], [$tag]); + } + + return []; + } } diff --git a/app/Services/CacheHelper.php b/app/Services/CacheHelper.php index 435f469..869503c 100644 --- a/app/Services/CacheHelper.php +++ b/app/Services/CacheHelper.php @@ -150,4 +150,12 @@ public function keyPaginate($table) { return sprintf(self::KEY_FORMAT, $table, 'paginate-' . request()->input('page', 1)); } + + /** + * @return string + */ + public static function keySiteSettings() + { + return 'site.settings'; + } } \ No newline at end of file diff --git a/app/helpers.php b/app/helpers.php index 9950343..7ef72a2 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -83,3 +83,13 @@ function isAdmin() return auth()->check() && auth()->user()->isAdmin(); } } + +if (!function_exists('setting')) { + /** + * @param $key + * @return mixed + */ + function setting($key) { + return array_get(app('settings'), $key); + } +} diff --git a/resources/views/categories/show.blade.php b/resources/views/categories/show.blade.php index 1a5e341..ca55545 100644 --- a/resources/views/categories/show.blade.php +++ b/resources/views/categories/show.blade.php @@ -4,8 +4,8 @@ {{ $category->name }} | @parent @endsection -@section('keywords'){!! $category->name !!}@endsection -@section('description'){!! $category->description !!}@endsection +@section('keywords'){{ $category->name }}@endsection +@section('description'){{ $category->description }}@endsection @section('content') @component('components.header') diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index e060768..f926606 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -15,8 +15,8 @@ @section('title'){{ config('app.name', 'Laravel') }}@show - - + + {{----}} {{----}} diff --git a/resources/views/posts/show.blade.php b/resources/views/posts/show.blade.php index fa5e019..6d53dab 100644 --- a/resources/views/posts/show.blade.php +++ b/resources/views/posts/show.blade.php @@ -4,8 +4,8 @@ {{ $post->title }} | @parent @endsection -@section('keywords'){!! $post->tags->implode('name', ',') !!}@endsection -@section('description'){!! $post->description !!}@endsection +@section('keywords'){{ $post->tags->implode('name', ',') }}@endsection +@section('description'){{ $post->description }}@endsection @section('content') @component('components.header') diff --git a/resources/views/tags/show.blade.php b/resources/views/tags/show.blade.php index 25e2a9a..3b1fa93 100644 --- a/resources/views/tags/show.blade.php +++ b/resources/views/tags/show.blade.php @@ -4,8 +4,8 @@ {{ $tag->name }} | @parent @endsection -@section('keywords'){!! $tag->name !!}@endsection -@section('description'){!! $tag->description !!}@endsection +@section('keywords'){{ $tag->name }}@endsection +@section('description'){{ $tag->description }}@endsection @section('content') @component('components.header')