Laravel 向每个呈现的视图添加内容
Laravel add content to every rendered view
有什么方法可以将内容添加到 Laravel 5.5 项目的每个渲染视图中吗?我想要一个 Composer 包,例如在不对实际项目代码进行任何修改的情况下向每个渲染视图添加一些 JavaScript 代码。
现在我已经通过在包中创建视图并将其包含到我的布局中来实现此功能 header,但如果无需手动包含即可以某种方式完成此操作会更好。
Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade's share method. Typically, you should place calls to share within a service provider's boot method. You are free to add them to the AppServiceProvider
or generate a separate service provider to house them
https://laravel.com/docs/5.5/views#sharing-data-with-all-views
或者您可以使用视图编辑器:
The composer method also accepts the *
character as a wildcard, allowing you to attach a composer to all views:
View::composer('*', function ($view) {
//
});
您可以为此使用中间件。您可以修改响应添加您想要的任何代码,例如此代码在 运行 测试时在 html 中的 </head>
之前添加额外的代码:
public function handle($request, Closure $next, $guard = null)
{
/** @var Response $response */
$response = $next($request);
if ($response instanceof Response && app()->runningUnitTests() &&
str_contains($response->headers->get('Content-Type'), 'text/html')) {
$content = $response->getContent();
if (($head = mb_strpos($content, '</head>')) !== false) {
$response->setContent(mb_substr($content, 0, $head) .
'<style>' . $this->config->get('laravel_test_css.style') . '</style>' .
mb_substr($content, $head));
}
}
return $response;
}
(这是我的中间件的一段代码 - 完整文件 - https://github.com/mnabialek/laravel-test-css/blob/master/src/Middleware/LaravelTestCss.php)
如果你想在全球范围内应用这个中间件,你可以这样做:
app('Illuminate\Contracts\Http\Kernel')->pushMiddleware(CustomMiddleware::class);
有什么方法可以将内容添加到 Laravel 5.5 项目的每个渲染视图中吗?我想要一个 Composer 包,例如在不对实际项目代码进行任何修改的情况下向每个渲染视图添加一些 JavaScript 代码。
现在我已经通过在包中创建视图并将其包含到我的布局中来实现此功能 header,但如果无需手动包含即可以某种方式完成此操作会更好。
Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade's share method. Typically, you should place calls to share within a service provider's boot method. You are free to add them to the
AppServiceProvider
or generate a separate service provider to house them
https://laravel.com/docs/5.5/views#sharing-data-with-all-views
或者您可以使用视图编辑器:
The composer method also accepts the
*
character as a wildcard, allowing you to attach a composer to all views:
View::composer('*', function ($view) {
//
});
您可以为此使用中间件。您可以修改响应添加您想要的任何代码,例如此代码在 运行 测试时在 html 中的 </head>
之前添加额外的代码:
public function handle($request, Closure $next, $guard = null)
{
/** @var Response $response */
$response = $next($request);
if ($response instanceof Response && app()->runningUnitTests() &&
str_contains($response->headers->get('Content-Type'), 'text/html')) {
$content = $response->getContent();
if (($head = mb_strpos($content, '</head>')) !== false) {
$response->setContent(mb_substr($content, 0, $head) .
'<style>' . $this->config->get('laravel_test_css.style') . '</style>' .
mb_substr($content, $head));
}
}
return $response;
}
(这是我的中间件的一段代码 - 完整文件 - https://github.com/mnabialek/laravel-test-css/blob/master/src/Middleware/LaravelTestCss.php)
如果你想在全球范围内应用这个中间件,你可以这样做:
app('Illuminate\Contracts\Http\Kernel')->pushMiddleware(CustomMiddleware::class);