当前位置:网站首页>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');
边栏推荐
- Development of trading system (III) - risk control system
- Development of trading system (II) -- market data
- DAP data scheduling function improvement description
- Numpy NP tips: use OpenCV to interpolate and zoom the array to a fixed shape cv2 resize(res, dsize=(64, 64), interpolation=cv2. INTER_ CUBIC)
- Trading system development (IV) - trading counter system
- DAP数据调度功能完善说明
- Simple integration of client go gin 11 delete
- Numpy NP tips: squeeze and other processing of numpy arrays
- Development of trading system (x) -- fix agreement
- Crawler grabs the data of Douban group
猜你喜欢
![[harmony OS] [arkui] ETS development graphics and animation drawing](/img/9d/0ac2b3d8bcdcd610767df930e2fa4e.png)
[harmony OS] [arkui] ETS development graphics and animation drawing

讲座记录《惯性导航的新应用——惯性测量》

Is opencv open source?

Summary of various problems encountered by cocos2d-x

【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示

SEO的5大关键指标:排名+流量+会话+停留时长+跳出率

(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

小心被偷脸!天天用的人脸识别风险原来这么多?

1.初识半音阶口琴

Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
随机推荐
[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube
Intel 13th generation core showed its true colors for the first time: 68mb cache improved significantly
长沙“求才”:“下力气”与“出实招”并进,“快发展”和“慢生活”兼得
智慧风电:数字孪生 3D 风机智能设备运维
Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand
论文阅读《LSD-SLAM: Large-Scale Direct Monocular SLAM》
Shutter fittedbox component
Text keyword extraction: ansj
升级cmake
numpy np tips: numpy数组的squeeze等处理
NFT insider 63: the sandbox reached a cooperation with Time magazine, and YGG established Spain's subdao
Numpy NP tips: squeeze and other processing of numpy arrays
WMS仓储管理系统的使用价值,你知道多少
Hello CTP (IV) - CTP transaction API
地方/园区产业规划之 “ 如何进行产业定位 ”
PHP代码审计1—PHP.ini的那些事
95% 程序员都在这里摸鱼……
Hello CTP (II) -- Introduction to CTP
PHP code audit 2 - these functions must be known and understood
cesium 图形标注圆形、正方形、多边形、椭圆等