Of course, returning full HTML document strings directly from your routes and controllers isn't realistic. Thankfully, views make it simple to divide all of our HTML into different files. Views are located in the resources/views
directory and separate your controller / application code from your display logic. A straightforward perspective may look like this:
<!-- View stored in resources/views/greeting.html -->
<html>
<body>
<h1>Hello, <%= name %></h1>
</body>
</html>
We can use the view function of response object to return this view, which is located in resources/views/greeting.html
Route.get('/', function ({view}) {
view('greeting', {'name' : 'James'});
});
Views can also be nested within the resources/views
directory's subdirectories. To refer to nested views, use the "dot" notation. If your view is located in resources/views/admin/profile.html
, for example, you may retrieve it from one of your application's routes / controllers like follows:
You may need to share data with all views displayed by your application on occasion. You may do so by using the share method on the View class. Typically, calls to the share method should be made within the boot
method of a service provider. You may either include them in the app/providers/appServiceProvider
class or create a new service provider for them:
const ServiceProvider = require('@ostro/support/serviceProvider')
class ViewServiceProvider extends ServiceProvider {
/**
* Register any application services.
*
* @return void
*/
register() {
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
boot() {
}
}