当前位置:网站首页>Laravel document sorting 7. View
Laravel document sorting 7. View
2022-06-25 04:19:00 【Angry devil】
Preface :Laravel Document sorting , Only for record , Nothing else .
1、 Directory of views
Resources/views
2、laravel How to return to the view
Route::get('/', function () {
return view('greeting', ['name' => 'James']);
});
Ps: adopt view() Method
3、 Usually in order to categorize views , It may be in the view directory , Create subdirectories , How to operate at this time ?
such as , There's this file :resources/views/admin/profile.php
Return as follows
return view('admin.profile', $data);
4、 How to determine whether a view exists
if (view()->exists('emails.customer')) {
//
}
Ps:
A、 Be careful , The judgment here , Is based on view() Method does not pass parameters , Just can use exists() Method
B、 When view() When a method is called without parameters , Will return a Illuminate\Contracts\View\Factory example , So that you can call factory Any method of
5、 How to transfer data to the view ?
return view('greetings', ['name' => 'Victoria']);
Ps: Notice the second parameter , Must be an array of key value pairs
6、 Another way to pass data to a view
$view = view('greeting')->with('name', 'Victoria');
7、 How to pass a data to all views ?
<?php
namespace App\Providers;
class AppServiceProvider extends ServiceProvider
{
/**
* Start any application's service .
*
* @return void
*/
public function boot()
{
view()->share('key', 'value');
}
/**
* Register service provider .
*
* @return void
*/
public function register()
{
//
}
}
Ps: Use factory Of share() Method . Usually , One parameter is passed to multiple views , It is written in the service provider boot In the way . It's written here AppServiceProvider in , perhaps , Generate a different service provider to place the code .
8、 What is a view component ?
View component , Before the view is rendered , Closures or class methods that will be called .
9、 Register view component instances within the service provider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Register all bindings in the container .
*
* @return void
*/
public function boot()
{
// Use view components of object type ...
view()->composer(
'profile', 'App\Http\ViewComposers\ProfileComposer'
);
// View components using closure type ...
view()->composer('dashboard', function ($view) {
});
}
/**
* Register service provider .
*
* @return void
*/
public function register()
{
//
}
}
Ps: The above is the use of view() Auxiliary function , Get the bottom layer Illuminate\Contracts\View\Factory Of contract Realization .
Be careful :laravel The framework does not specify a default view component Directory . You can customize . Above , Is defined in
App\Http\ViewComposers Catalog
Attention : If you create a new service provider that contains view components , You need to add service providers to config/app.php Of documents providers Array .
Register the view component , Each time the profile When the view is rendered ,[email protected] Methods will be run .
Next, let's look at how to define the component :
<?php
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
use Illuminate\Users\Repository as UserRepository;
class ProfileComposer
{
/**
* Instances of user objects .
*
* @var UserRepository
*/
protected $users;
/**
* Create a new personal data view component .
*
* @param UserRepository $users
* @return void
*/
public function __construct(UserRepository $users)
{
// All dependencies are automatically resolved by the service container ...
$this->users = $users;
}
/**
* Bind data to view .
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$view->with('count', $this->users->count());
}
}
Before rendering the view ,composer Method will be called , And introduce a Illuminate\Contracts\View\View example , You can also pass with() Method .
10、 How to use the same component for multiple views ?
view()->composer(
['profile', 'dashboard'],
'App\Http\ViewComposers\MyViewComposer'
);
View's composer Method acceptable * As a wildcard , That is, attach... To all views composer
view()->composer('*', function ($view) {
//
});
11、 What is a view Creator , What's the effect ?
The benefits of view creators , The view runs immediately after initialization , And the view component , Wait until the view is rendered .
12、 How to register a Creator ?
view()->creator('profile', 'App\Http\ViewCreators\ProfileCreator');
边栏推荐
- 驻波比计算方法
- Cesium drag 3D model
- DAP数据调度功能完善说明
- Siddhartha: the book of life can be regurgitated frequently
- numpy np tips:使用opencv对数组插值放缩到固定形状 cv2.resize(res, dsize=(64, 64), interpolation=cv2.INTER_CUBIC)
- Flutter Builder & FutureBuilder组件
- 数学分析_笔记_第3章:极限
- 【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示
- @Requestbody solution get parameter is null
- Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
猜你喜欢

(ultra detailed onenet TCP protocol access) arduino+esp8266-01s accesses the Internet of things platform, uploads real-time collected data /tcp transparent transmission (and how to obtain and write Lu

Cesium drag 3D model

Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL

95% 程序员都在这里摸鱼……

地方/园区产业规划之 “ 如何进行产业定位 ”

数字时代的“文艺复兴”?起底数字藏品,让人欢喜让人愁

Crawl Sina Weibo fans

NFT Insider #63:The Sandbox与时代杂志达成合作,YGG成立西班牙subDAO

DAP data scheduling function improvement description

讲座记录《惯性导航的新应用——惯性测量》
随机推荐
2. play the chromatic harmonica
PHP代码审计1—PHP.ini的那些事
微信小程序中的列表渲染
Smart contract learning materials
Although the Internet in the traditional sense has long ceased to exist, this does not mean that the Internet has long disappeared
Hello CTP (IV) - CTP transaction API
NFT insider 63: the sandbox reached a cooperation with Time magazine, and YGG established Spain's subdao
Color NFT series representing diversity launched on the sandbox market platform
AI quantitative transaction (I) -- Introduction to quantitative transaction
Development of trading system (I) -- Introduction to trading system
【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示
学习码 滚动码 固定码 有什么区别重码数,编码容量滚动码的原理
"How to carry out industrial positioning" in local / Park industrial planning
General steps for QT compiling database plug-ins
地方/园区产业规划之 “ 如何进行产业定位 ”
Development of trading system (IX) -- dark pool technology
【LeetCode】22. 括号生成
Numpy NP tips: squeeze and other processing of numpy arrays
Cesium 拖拽3D模型
【LeetCode】143. 重排链表