Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Laravel assign values to public templates

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how Laravel assigns values to public templates". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Laravel assigns values to public templates.

Many times during the development process, values are assigned to common templates, such as the top navigation bar, the bottom of the page, etc., and it is impossible to assign values in every controller.

The solution in Laravel is as follows: modify

App\ Providers\ AppServiceProvider

Add in the boot method

View ()-> composer ('common.header',function ($view) {/ / common.header corresponds to Blade template $view- > with (' key', 'value');})

You can also assign values to all templates

View ()-> share ('key',' value')

View composers is related to the view and is used in a boot () function of service provider, that is, when a view is loaded, due to the role of view composer, to call a function to pass a parameter or something.

1, create service provider

Php artisan make:provider ComposerServiceProvider

Then add ComposerServiceProvider to config/app.php.

2, write view composer

Public function boot () {view ()-> composer ('app', / / template name' App\ Http\ ViewComposers\ MovieComposer' / / method name or method in class);}

Once app.blade.php is loaded, execute the composer function in App\ Http\ ViewComposers\ MovieComposer (the composer function is the default here), and if you want to change it,

View ()-> composer ('app','App\ Http\ ViewComposers\ MovieComposer@foobar'); / / self-defined method

This is where the foobar function is executed

Write this in App\ Http\ ViewComposers\ MovieComposer.php

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report