1
Q

How can you create a view using Artisan?

A

php artisan make:view example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you manually create a view without using Artisan?

A

You may create a view by placing a file with the .blade.php extension in your application’s resources/views directory. The .blade.php extension informs the framework that the file contains a Blade template.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do Blade templates allow you to do?

A

Blade templates contain HTML as well as Blade directives that allow you to easily echo values, create “if” statements, iterate over data, and more

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which helper function allows you to display one of your Blade templates in your application’s routes or controllers?

A

The global view helper:

Route::get('/', function () {
    return view('example', ['example_argument' => 'blue']);
});

Views may also be returned using the View facade:
return View::make('example', ['example_argument' => 'red']);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you nest views within subdirectories and call upon them?

A

You can just put views into directories within the resources/views directory and to call upon them you use dot notation. For example, if your view is stored at resources/views/example/exampleview.blade.php, you may return it from one of your application’s routes / controllers like so:
return view('admin.profile', $data);

View directory names should NOT contain the . character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you create the first available view that exists in a given array of views?

A

Using the View facade’s first method, you may create the first view that exists in a given array of views. This may be useful if your application or package allows views to be customized or overwritten:
return View::first(['custom.admin', 'admin'], $data);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you determine if a view exists?

A

You may use the View facade’s exists method. It will return true if the view exists:

if (View::exists('admin.profile')) {
    // ...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you make data available to a view?

A

You may pass an array of data to views to make the data available to the view:
return view('example', ['ex_key' => 'ex_value']);
When passing information in this manner, the data should be an array with key / value pairs. After providing data to a view, you can then access each value within your view using the data’s keys, such as <?php echo $name; ?>

As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view. The with method returns an instance of the view object so that you can continue chaining methods before returning the view:
return view('greeting')->with('name', 'Mustermann')->with('first_name', 'Max');

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Occasionally, you may need to share data with all views that are rendered by your application. Which method lets you do that where?

A

You may do so using the View facade’s share method. Typically, you should place calls to the share method within a service provider’s boot method. You are free to add them to the App\Providers\AppServiceProvider class or generate a separate service provider to house them:

class AppServiceProvider extends ServiceProvider
{
    // ...
    public function boot(): void
    {
        View::share('key', 'value');
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are view composers?

A

View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location. View composers may prove particularly useful if the same view is returned by multiple routes or controllers within your application and always needs a particular piece of data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Where are view composers registered?

A

Typically, view composers will be registered within one of your application’s service providers. As an example, it could be the AppServiceProvider.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you register a view composer and where are they organized?

A

By using the View facade’s composer method to register the view composer. Laravel does not include a default directory for class based view composers, so you are free to organize them hoewver you wish. For example, you could create an app/View/Composers directory to house all of your application’s view composers. Let’s register an example composer in the AppServiceProviders boot method:

public function boot(): void
    {
        // Using class based composers...
        Facades\View::composer('profile', ProfileComposer::class);
 
        // Using closure based composers...
        Facades\View::composer('welcome', function (View $view) {
            // ...
        });
 
        Facades\View::composer('dashboard', function (View $view) {
            // ...
        });
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you attach a view composer to multiple views?

A

You may attach a view composer to multiple views at once by passing an array of views as the first argument to the composer method:

View::composer(
    ['profile', 'dashboard'],
    MultiComposer::class
);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you attach a view composer to all views?

A

The composer method also accepts the * character as a wildcard, allowing you to attach a composer to all views:

Facades\View::composer('*', function (View $view) {
    // ...
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between a view composer and a view creator?

A

View creators are very similar to view composers; however, they are executed immediately after the view is instantiated instead of waiting until the view is about to render

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you register a view creator?

A

To register a view creator, use the creator method:
View::creator('example', ExampleCreator::class);