当前位置:网站首页>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');

原网站

版权声明
本文为[Angry devil]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210535177824.html