Docs / The Basics / Views
Views
Introduction
Of course, it's not practical to return entire HTML documents strings directly from your routes and controllers. Thankfully, views provide a convenient way to place all of our HTML in separate files.
Views separate your controller / application logic from your presentation logic. In Laraflare, the screens of your application are Vue components stored in resources/js/pages, and there is no Blade templating language. The server-side view factory that remains, stored in resources/views, renders the templates the framework itself sends, such as Markdown mail templates and the legal pages, and is available to your own code on Node.
Note
Looking for more information on how to write screens? Check out the frontend documentation to get started.
Writing Views in Vue
A Laraflare application writes its frontend templates in Vue, bundled with Vite. A screen is a component in resources/js/pages, and the client-side router in resources/js/router.ts matches the browser's address to it. A controller returns data, such as JSON, and the screen reads it by calling your application's routes. See the frontend documentation and the starter kits.
Creating and Rendering Views
You may create a screen by placing a file with the .vue extension in your application's resources/js/pages directory or by using the make:view Artisan command:
npx artisan make:view greeting
The command writes resources/js/pages/greeting.vue; its --extension option changes the extension, and --test adds a feature test that visits the page.
Views for the server-side view factory are files in resources/views. Once a view exists there, you may render it with the global view helper:
import { view } from '@laraflare/framework';
const html = view('greeting', { name: 'James' });
The first argument passed to the view helper corresponds to the name of the view file in the resources/views directory. The second argument is an object of data that should be made available to the view. Called with no arguments, view() returns the view factory itself, which the following sections use.
Note
A Worker has no folder to read views from, so
npx artisan view:cachewritesresources/views/index.ts, which the Worker bundle carries. The view service provider is registered on Node only (Artisan and tests).
Nested View Directories
Views may also be nested within subdirectories of the resources/views directory. "Dot" notation may be used to reference nested views. For example, if your view is stored at resources/views/admin/profile, you may render it like so:
return view('admin.profile', data);
Warning
View directory names should not contain the
.character.
Creating the First Available View
Using the view factory'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:
import { view } from '@laraflare/framework';
return (view() as any).first(['custom.admin', 'admin'], data);
Determining if a View Exists
If you need to determine if a view exists, you may use the view factory. The exists method will return true if the view exists:
import { view } from '@laraflare/framework';
if ((view() as any).exists('admin.profile')) {
// ...
}
Passing Data to Views
As you saw in the previous examples, you may pass an object of data to views to make that data available to the view:
return view('greetings', { name: 'Victoria' });
When passing information in this manner, the data should be an object with key / value pairs. After providing data to a view, you can then access each value within your view using the data's keys.
As an alternative to passing a complete object 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 the view so that you can continue chaining methods before returning it:
return (view('greeting') as any)
.with('name', 'Victoria')
.with('occupation', 'Astronaut');
Sharing Data With All Views
Occasionally, you may need to share data with all views that are rendered by your application. You may do so using the view factory'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 AppServiceProvider class in app/Providers or generate a separate service provider to house them:
import { ServiceProvider, view } from '@laraflare/framework';
export class AppServiceProvider extends ServiceProvider {
/**
* Register any application services.
*/
override register(): void {
// ...
}
/**
* Bootstrap any application services.
*/
override boot(): void {
(view() as any).share('key', 'value');
}
}
View Composers
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.
Typically, view composers will be registered within one of your application's service providers. In this example, we'll assume that the AppServiceProvider will house this logic.
We'll use the view factory's composer method to register the view composer. Laraflare does not include a default directory for class-based view composers, so you are free to organize them however you wish. For example, you could create an app/View/Composers directory to house all of your application's view composers:
import { ServiceProvider, view } from '@laraflare/framework';
import { ProfileComposer } from '@/app/View/Composers/ProfileComposer';
export class AppServiceProvider extends ServiceProvider {
/**
* Bootstrap any application services.
*/
override boot(): void {
const views = view() as any;
// Using class-based composers...
views.composer('profile', ProfileComposer);
// Using closure-based composers...
views.composer('welcome', (view: any) => {
// ...
});
views.composer('dashboard', (view: any) => {
// ...
});
}
}
Now that we have registered the composer, the compose method of the ProfileComposer class will be executed each time the profile view is being rendered. Let's take a look at an example of the composer class:
import { UserRepository } from '@/app/Repositories/UserRepository';
export class ProfileComposer {
/**
* Create a new profile composer.
*/
constructor(protected users: UserRepository) {}
/**
* Bind data to the view.
*/
compose(view: any): void {
view.with('count', this.users.count());
}
}
As you can see, all view composers are resolved via the service container, so you may type-hint any dependencies you need within a composer's constructor.
Attaching a Composer to Multiple Views
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() as any).composer(
['profile', 'dashboard'],
MultiComposer,
);
The composer method also accepts the * character as a wildcard, allowing you to attach a composer to all views:
(view() as any).composer('*', (view: any) => {
// ...
});
View Creators
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. To register a view creator, use the creator method:
(view() as any).creator('profile', ProfileCreator);
Optimizing Views
Screens are compiled by Vite when you run npm run build, which wrangler does when it uploads your application, so they need no step of their own.
Server-side views are compiled on demand. Laraflare provides the view:cache Artisan command to precompile all of the views utilized by your application. For increased performance, you may wish to run this command as part of your deployment process:
npx artisan view:cache
You may use the view:clear command to clear the view cache:
npx artisan view:clear