laravel-composer

laravel 视图组件、服务注入

视图组件就是在视图被渲染前,会被调用的闭包或类方法。如果你想在每次渲染某些视图时绑定数据,视图组件可以帮你把这样的程序逻辑都组织到同一个地方。
我们需要在我们的服务提供者中注册一个视图 composer 则应该在 boot 方法中完成。此方法会在所有其它的服务提供者被注册后才被调用,意味着你能访问已经被框架注册的所有其它服务

请注意,Laravel 没有默认的目录来放置视图组件。你可以随意把它们放到任何地方。
你可以创建一个 App\Http\ViewComposers 目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
/**
* 视图 composer 共享后台菜单数据
* 多个视图共用一个视图组件
*/
view()->composer(
['layouts.index','layouts.blog', 'front.article.detail', 'front.category.list' , 'front.index.search'], 'App\Http\ViewComposers\SettingsComposer'
);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
namespace App\Http\ViewComposers;
use Illuminate\View\View;
class SettingsComposer
{
/**
* 将数据绑定到视图。
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
// getSettings() 方法如下
$view->with('settings', getSettings());
}
}

getSettings() 方法如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 这是 laravel 5.3 版本
if(!function_exists('getSettings')){
function getSettings()
{
$key = config('admin.global.blog');

// has 方法可以用来检查一个项目是否存在于缓存中
if (cache()->has($key)) {
return cache($key);
}else{
// 此方法需要安装包 edvinaskrucas/settings
$settings = settings($key,config('admin.global.setting'));
// forever 方法可以用来存放永久的项目到缓存中,这些值必须被手动的删除,这可以通过 forget 方法实现
cache()->forever($key,$settings);
return $settings;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
// 这是 laravel 5.2 版本

use Illuminate\Support\Facades\Cache;

if(!function_exists('getSettings')){
function getSettings()
{
$key = config('admin.global.blog');

if (Cache::has($key)) {
return Cache::get($key);
}else{
$settings = settings($key,config('admin.global.setting'));
Cache::forever($key,$settings);
return $settings;
}
}
}

使用

1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>{{$settings['title']}}</title>
<meta name="keywords" content="{{$settings['keywords']}}" />
<meta name="description" content="{{$settings['description']}}">
<meta name="author" content="{{$settings['author']}}">

缓存

缓存实例看手册
缓存接口

源码 cache() 方法如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
if (! function_exists('cache')) {
/**
* Get / set the specified cache value.
*
* If an array is passed, we'll assume you want to put to the cache.
*
* @param dynamic key|key,default|data,expiration|null
* @return mixed
*
* @throws \Exception
*/
function cache()
{
$arguments = func_get_args(); // 返回一个包含函数参数列表的数组

if (empty($arguments)) {
return app('cache');
}

if (is_string($arguments[0])) {
// get 方法可以用来取出缓存中的项目,缓存不存在的话返回 null,get 方法接受第二个参数,作为找不到项目时返回的预设值
return app('cache')->get($arguments[0], isset($arguments[1]) ? $arguments[1] : null);
}

if (is_array($arguments[0])) {
if (! isset($arguments[1])) {
throw new Exception(
'You must set an expiration time when putting to the cache.'
);
}

/**
* put 方法来存放项目到缓存中,你需要使用第三个参数来设定缓存的存放时间
* key — 从关联数组中取得键名
* reset — 将数组的内部指针指向第一个单元,取值
*/
return app('cache')->put(key($arguments[0]), reset($arguments[0]), $arguments[1]);
}
}
}

edvinaskrucas/settings

执行 composer require edvinaskrucas/settings
包地址

服务注入

@inject 命令可以取出 Laravel 服务容器 中的服务。传递给 @inject 的第一个参数为置放该服务的变量名称,而第二个参数为你想要解析的服务的类或是接口的名称

1
2
3
4
5
@inject('metrics', 'App\Services\MetricsService')

<div>
每月收入:{{ $metrics->monthlyRevenue() }}。
</div>

参考学习

视图组件

如果对您有用,请博主喝杯咖啡!

热评文章