当前位置:网站首页>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');
边栏推荐
- "Comment positionner l'industrie" dans la planification industrielle locale / parc
- opencv是开源的吗?
- 讲座记录《多种空间大地测量技术的数据处理方法和应用》
- MySQL order by
- WMS仓储管理系统的使用价值,你知道多少
- 佐喃社区
- 驻波比计算方法
- General steps for QT compiling database plug-ins
- Numpy NP tips: squeeze and other processing of numpy arrays
- 5 key indicators of SEO: ranking + traffic + session + length of stay + bounce rate
猜你喜欢

无法安装redis接口

Watch out for the stolen face! So many risks of face recognition used every day?

Hello CTP (II) -- Introduction to CTP

Summary of various problems encountered by cocos2d-x

Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand

Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!

Color NFT series representing diversity launched on the sandbox market platform

Is opencv open source?

Vigilance against over range collection of privacy - ten mobile app violations

1. Phase II of the project - user registration and login
随机推荐
opencv是开源的吗?
Development of trading system (XIII) -- Analysis of quickfix source code
Where is the red area of OpenCV?
1280_ C language to find the average value of two unsigned integer
智慧风电:数字孪生 3D 风机智能设备运维
How to install opencv? Opencv download installation tutorial
Uniapp makes mobile app programs, using uni Choosevideo record video, video playback is fuzzy, and the resolution is low
Development of trading system (VI) -- HFT high frequency trading
Lecture record: history and development of strapdown inertial navigation solution
MySQL插入过程报错1062,但是我没有该字段。
Standing wave ratio calculation method
(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
"Comment positionner l'industrie" dans la planification industrielle locale / parc
1. first knowledge of chromatic harmonica
2.吹响半音阶口琴
冷热酸甜、想成就成?冷酸灵母公司登康口腔欲在深交所主板上市
练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
Smart wind power: operation and maintenance of digital twin 3D wind turbine intelligent equipment
SQL, CTE, flg case problems
Lecture record: data processing methods and applications of various spatial geodetic techniques