Home Github
Laravel Logo

Using Pesto with Laravel

Integrate the Pesto template engine into your Laravel projects to create expressive and modern views.

Integration Steps

1. Install Pesto

Start by installing Pesto via Composer.

composer require millancore/pesto

2. Create a Service Provider

Create a service provider at app/Providers/PestoServiceProvider.php to register Pesto in Laravel's container.

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use MillanCore\Pesto\Pesto;
use MillanCore\Pesto\PestoFactory;

class PestoServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(Pesto::class, function ($app) {
            return PestoFactory::create(
                resource_path('views'),
                storage_path('framework/cache/pesto')
            );
        });
    }
}

3. Register the Service Provider

Add the provider to your bootstrap/providers.php array.

return [
    App\Providers\AppServiceProvider::class,
    App\Providers\PestoServiceProvider::class,
];

4. Create a Helper Function

Add a global pestoView() helper to make rendering even simpler. Create a app/helpers.php file and register it in your composer.json.

// app/helpers.php

use Illuminate\Http\Response;
use MillanCore\Pesto\Pesto;

function pestoView(string $view, array $data = [], int $status = 200, array $headers = []): Response
{
    $html = app(Pesto::class)->make($view, $data)->toHtml();

    return response($html, $status, $headers);
}

Then add the autoload entry in your composer.json:

"autoload": {
    "files": [
        "app/helpers.php"
    ]
}

Run composer dump-autoload after adding the entry.

5. Render a View in Your Controller

Now you can use the pestoView() helper anywhere in your application.

namespace App\Http\Controllers;

class HomeController extends Controller
{
    public function index()
    {
        return pestoView('home.php', [
            'title' => 'Welcome to Laravel with Pesto',
        ]);
    }
}

6. Create a Pesto Template

Create your template at resources/views/home.php. You can now use Pesto's syntax.

<h1>{{ $title }}</h1>

<ul>
    <li php-foreach="range(1, 5) as $i">
        Item {{ $i }}
    </li>
</ul>