Views
Introduction
Views in Laravel are responsible for separating the application's logic from the presentation layer. They provide a clean way to manage and organize the HTML content of your application. Views are typically stored in the resources/views directory and are rendered using the Blade templating engine, which offers a simple and powerful way to create dynamic content.
By using views, you can create reusable templates and components, making your code more maintainable and easier to understand. Blade templates allow you to use control structures like loops and conditionals, as well as to include other templates, which helps to keep your views organized and modular.
To learn in detail about Views, you can visit the Laravel documentation here.
Here's a basic example of a Blade template:
Directory Structure
To organize the views for our example package, we need to set up a specific directory structure. Follow the steps below to create the necessary folders.
Create the Resources Folder
- Navigate to the
packages/Webkul/Example/srcdirectory. - Create a folder named
Resources.
Create the views Folder
- Inside the
Resourcesfolder, create another folder namedviews.
Create the example Folders
- Inside the
viewsfolder, create two folders namedexample.
The updated directory structure will look like this:
└── packages
└── Webkul
└── Example
└── src
├── ...
└── Resources
└── views
└── exampleAdding HTML Content
Below is an example of basic HTML content that you can add to the example index.blade.php file.
index.blade.php in the example Folder
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<h1>Example</h1>
<p>Welcome to the example section for managing example content.</p>
</body>
</html>Load Views from Package
To make the views in our package accessible, we need to register them in the service provider's boot method. This involves updating the ExampleServiceProvider.php file to include the view loading logic. Follow the steps below:
Open the Service Provider File
- Navigate to the
packages/Webkul/Example/src/Providersdirectory. - Open the
ExampleServiceProvider.phpfile.
Update the boot Method
- Inside the
bootmethod of theExampleServiceProviderclass, add the logic to load views from the package.
Updated ExampleServiceProvider.php
Here is the updated code for ExampleServiceProvider.php:
<?php
namespace Webkul\Example\Providers;
use Illuminate\Support\ServiceProvider;
class ExampleServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//...
$this->loadViewsFrom(__DIR__ . '/../Resources/views', 'example');
}
}Explanation
- The
namespacekeyword defines the namespace for theExampleServiceProviderclass, which isWebkul\Example\Providers. - The
ExampleServiceProviderclass extends Laravel's baseServiceProviderclass. - The
bootmethod is used to bootstrap any application services. - Inside the
bootmethod, we use the$this->loadViewsFrommethod to register the views from the package. - The
loadViewsFrommethod takes two arguments:- The path to the views directory within the package:
__DIR__ . '/../Resources/views'. - A namespace for the views:
'example'.
- The path to the views directory within the package:
Rendering Views
In Laravel applications, views are typically rendered from controller methods using the view helper function. This section describes how views are invoked and passed data from a controller.
<?php
namespace Webkul\Example\Http\Controllers\Shop;
use Webkul\Example\Http\Controllers\Controller;
use Webkul\Example\Repository\ExampleRepository;
class ExampleController extends Controller
{
/**
* Create a controller instance.
*
* @param \Webkul\Example\Repository\ExampleRepository $exampleRepository
* @return void
*/
public function __construct(protected ExampleRepository $exampleRepository)
{
}
/**
* Index.
*
* @return \Illuminate\View\View
*/
public function index()
{
$examples = $this->exampleRepository->with(['author'])->all();
return view('example::example.index', compact('examples'));
}
}Explanation
The
viewhelper function in Laravel is used within theindexmethod of theExampleControllerto render theexample.indexview.It accepts two parameters the name of the view (
example::example.index) and an array of data (compact('examples')) to pass to theview.
Blade File Naming Convention
UnoPim utilizes Blade templates to handle listing, creation, and updating operations for resources like products, examples, and categories. This section provides a detailed guide on how to implement these operations using Blade templates within your UnoPim package.
Listing (Index Blade):
The
index.blade.phptemplate is used to display a list of all records (examples).The controller's
indexmethod fetches all examples and passes them to the view.
Creation (Create Blade):
The
create.blade.phptemplate contains a form for creating new records.The controller's
createmethod renders this view.
Updating (Edit Blade):
The
edit.blade.phptemplate contains a form for editing existing records.The controller's
editmethod fetches the specific example and passes it to the view.
By following these steps, you can effectively utilize Blade templates in UnoPim for listing, creating, and updating resources, ensuring a structured and maintainable approach to managing CRUD operations within your application.