Creation and data transfer of Larave view


Create View

The view contains the HTML code of the application, and separates the controller logic and presentation logic of the application. View files are stored in resources/views catalog. Here is an example of a simple view:

 <!--  This view stores resources/views/greeting. blade. php --> <html> <body> <h1>Hello, {{ $name }}</h1> </body> </html>

Because this view is stored in resources/views/greeting.blade.php , we can use auxiliary functions view Return the view response like this:

 Route::get('/', function () { Return view ('greeting ', ['name'=>'Scholar']); });

As you can see, it is passed to view The first parameter of the method is resources/views The name of the corresponding view file in the directory. The second parameter is an array, which contains all the valid data in the view. In this example, we pass a name Variables, using Blade syntax Show it.

Of course, views can also be stored in resources/views The "." symbol is used to reference nested views in the subdirectory of. For example, if the view storage path is resources/views/admin/profile.blade.php , we can quote it as follows:

 return view('admin.profile', $data);

Determine whether the view exists

If you need to determine whether the view exists, call View On the facade exists Method. If the view exists on the disk, it returns true

 use Illuminate\Support\Facades\View; if (View::exists('emails.customer')) { // }

Create the first valid view

call View On the facade first Method can be used to create the first existing view in the given view array:

 use Illuminate\Support\Facades\View; return View::first(['custom.admin', 'admin'], $data);

This function is useful when applications or expansion packs allow views to be customized or overwritten.

Transfer Data to View

In the above example, we can see that we can simply transfer data to the view by array:

 Return view ('greetings', ['name '=>' Scholar ']);

When data is transferred in this way, $data It should be a key value pair array. In the view, you can use the corresponding key to access data values, such as <? php echo $key; ?> In addition, you can also use the with Method to add independent data fragments to the view:

 $view=view ('greeting ') ->with ('name', 'scholar');

Share data between views

Sometimes, we need to share data fragments among all views. At this time, we can use the share Method. Usually, you need to use the boot Method share Method, you can add it to AppServiceProvider Or generate an independent service provider to store this code logic:

 <? php namespace App\Providers; use View; class AppServiceProvider extends ServiceProvider { /** *Start all application services * * @return void */ public function boot() { View::share('key', 'value'); } /** *Registered Service Provider * * @return void */ public function register() { // } }

View Composer

View Composer is a callback function or class method when a view is rendered. If you have some data to bind each time you render the view, you can use View Composer to organize the logic into a separate place.

In this example, first register the view Composer in a service provider. We will use View Access from the front Illuminate\Contracts\View\Factory Remember that Larravel does not contain the default view Composer directory. We can organize its path according to our own preferences, for example, we can create a app/Http/ViewComposers catalog:

 <? php namespace App\Providers; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; class ComposerServiceProvider extends ServiceProvider { /** *Register the binding in the container * * @return void * @author  https://xueyuanjun.com */ public function boot() { //Using Composers Based on Class Methods View::composer( 'profile', 'App\Http\ViewComposers\ProfileComposer' ); //Using Composers Based on Callback Functions View::composer('dashboard', function ($view) {}); } /** *Registered Service Provider * * @return void */ public function register() { // } }
Note: If you create a new service provider to include the view Composer registration, you need to add the service provider to the configuration file config/app.php Of providers Array.

Now that we have registered the view Composer, every time profile Executes when the view is rendered ProfileComposer@compose Method, let's define the Composer class:

 <? php namespace App\Http\ViewComposers; use Illuminate\View\View; use Illuminate\Repositories\UserRepository; class ProfileComposer { /** *User warehouse implementation * * @var UserRepository */ protected $users; /** *Create a new attribute composer * * @param UserRepository $users * @return void */ public function __construct(UserRepository $users) { //Dependency injection is automatically resolved through the service container $this->users = $users; } /** *Bind data to the view * * @param View $view * @return void */ public function compose(View $view) { $view->with('count', $this->users->count()); } }

Before the view is rendered, the Composer class compose Method is called, while Illuminate\View\View Instance is injected into the method so that its with Method to bind data to the view.

Note: All view Composers are resolved through the service container, so you can declare any dependency you need in the constructor of Composer class.

Add Composer to Multiple Views

You can pass an array of views as composer Method to add the view Composer to multiple views at one time:

 View::composer( ['profile', 'dashboard'], 'App\Http\ViewComposers\MyViewComposer' );

composer The method also supports * Wildcard characters to allow adding a Composer to all views:

 View::composer('*', function ($view) { // });

View Creator

The view creator is very similar to the view composer. The difference is that the former fails immediately after the view is instantiated rather than waiting until the view is about to render. use View Facade creator Method to register a view creator:

 View::creator('profile', 'App\Http\ViewCreators\ProfileCreator');

give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: Larravel view rendering: Blade template engine

>>Next: Auxiliary Functions: Request URL Generation