What are views in Laravel


Views provide convenience for separating presentation logic and application logic. All views in Laravel are saved in the Sub Contents.

Basic usage

A simple view looks like this:

We can use the global help function. Come back to the view. The result of the rendering:

you can see the first parameter the method receives is related to the directory filename. This method also allows an array to be passed as a second parameter, and the key-value pairs in the array are passed to the view as corresponding variables. The example above is output

If the view is there

If you need to determine if the view exists, you can use it to verify you can use the parameterless global helper function If the view exists for chain operation it will be returned :

On call, if the method doesn't pass parameters, it returns a. Any contract method can be accessed.

Show data

Pass data to the view

As you can see from the example above, you can simply use an array as data to pass it to the view:

Use this method to pass data. It must be in the form of key-value pairs. In the view you can use the corresponding "key" to access the corresponding value. Of course you can use method for passing additional data:

Share data with all views

Sometimes you may need to share some of the data with all views. You can use the View Factory method to do this. Typically, you should be in the service container method for performing data sharing operations. You can or independently generate a separate service provider for this:

Show composers

View composers (players) are callback methods or class methods that are triggered when the view is rendered. If you want the view to automatically bind some data to the view each time it is rendered, the View Composer can separate that logical processing.

Now let's register our own View composers with the service provider. We will use auxiliary method to return a An example of the realization of the contract. Laravel does not provide a default directory for managing these view composers. You can manage these as you wish. Of course, you can also create a table of contents:

If you are creating a service provider that will hold the registry of all View Composers, make sure to remember to add the provider to the array in the file.

Now we have the registered composer method Will be in sight Will be executed automatically every time it is rendered. Next we define class:

Whenever the view is rendered, the composer methods are called and consist of an instance that you can use method to add additional data to the view.

Note: All View Composers are resolved through the service container, so you can add type hints information in the Composer constructor to perform any dependency injection.

Add the composer to multiple views

You can bind a composer to several views at the same time.The first parameter in the method is passed an array containing the name of the view:

Actually method also accepts characters are used as placeholders so that you can append the composer to all views:

Show creator

The view creator is similar to the view composer, except that it triggers the registration method immediately after the view is instantiated rather than waiting for the view to be rendered. You can use method to register the creator of the view: