Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alex_storm's avatar

How to remove ViewServiceProvider in laravel 11?

Hi! I want to remove the entire frontend from the project and leave only the backend. In previous versions it was very easy to do this. And now there are problems.

I excepted this provider and removed it from app, routes, etc..

// config/app.php
'providers' => ServiceProvider::defaultProviders()
        ->except([
            \Illuminate\View\ViewServiceProvider::class,
        ])
        ->merge([
            \App\Providers\AppServiceProvider::class,
        ])
        ->toArray(),

But .. I still get message - Target class [view] does not exist. I don't understand how create default backend application on laravel 11 without any another frontend like nodejs, view, blade, etc... only what I need.

I didn't find any information how exclude it(

0 likes
8 replies
puklipo's avatar

There's no need to do anything unnecessary.

Even if you don't intend to use view, you may end up using it for Mail or Notifications.

alex_storm's avatar

@puklipo sorry, but i would like to disable it, and disable another too.. This is an unnecessary addiction.

'providers' => ServiceProvider::defaultProviders()
        ->except([
            \Illuminate\View\ViewServiceProvider::class,
            \Illuminate\Mail\MailServiceProvider::class,
            \Illuminate\Broadcasting\BroadcastServiceProvider::class,
            \Illuminate\Bus\BusServiceProvider::class,
            \Illuminate\Notifications\NotificationServiceProvider::class,
            \Illuminate\Pagination\PaginationServiceProvider::class,
            \Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
            \Illuminate\Redis\RedisServiceProvider::class,
        ])
        ->merge([
            \App\Providers\AppServiceProvider::class,
        ])
        ->toArray(),

I would like to make something like Lumen.. only for API... But now it is no longer possible to do this

alex_storm's avatar

@jlrdw How middleware can resolve except unnecessary providers? This is difference logic. Middleware by default - OK. But I can disablle this middleware too.

rodrigo.pedra's avatar
Level 56

Override the web middleware group to not include the Illuminate\View\Middleware\ShareErrorsFromSession middleware. This middleware calls the View component to share the $errors global variable.

See this to learn how to do it:

https://laravel.com/docs/11.x/middleware#manually-managing-laravels-default-middleware-groups

You will also need to ask Laravel to always render exceptions as JSON, on your ./bootstrap/app.php

->withExceptions(function (Exceptions $exceptions) {
    $exceptions->shouldRenderJsonWhen(static fn () => true);
})

As the default exception handler needs the View component.

rodrigo.pedra's avatar

@alex_storm

I have this setup on a fresh Laravel 11 project:

file: ./bootstrap/app.php

Note that I am not prefixing my routes with /api

<?php

use App\Http\Middleware\ForceJsonResponse;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Routing\Router;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(function (Router $router) {
        $router->middleware(['api'])->group(__DIR__ . '/../routes/api.php');
    })
    ->withCommands([__DIR__ . '/../routes/console.php'])
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->prepend(ForceJsonResponse::class);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        $exceptions->dontReportDuplicates();
        $exceptions->shouldRenderJsonWhen(static fn () => true);
    })
    ->withEvents(discover: false)
    ->create();

file: ./app/Http/Middleware/ForceJsonResponse.php

This will make all requests return JSON, as Illuminate\Http\Request@wantsJson checks for the Accept header.

If your API returns different content types than JSON, you might need to tweak this middleware to exclude the related routes.

<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class ForceJsonResponse
{
    public function handle(Request $request, \Closure $next): Response
    {
        $request->header('Accept', 'application/json');

        return $next($request);
    }
}

file: ./config/app.php

Added this key to prevent Laravel from adding default providers

'providers' => [],

file: ./bootstrap/providers.php

Instead of using ServiceProvider::defaultProviders() and then removing the ones I don't need, I just added the ones I needed.

<?php

use App\Providers\AppServiceProvider;

return [
    Illuminate\Cache\CacheServiceProvider::class,
    Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
    Illuminate\Database\DatabaseServiceProvider::class,
    Illuminate\Filesystem\FilesystemServiceProvider::class,
    Illuminate\Foundation\Providers\FoundationServiceProvider::class,
    Illuminate\Validation\ValidationServiceProvider::class,

    AppServiceProvider::class,
];

[Bonus] file: ./routes/console.php

Added these commands, as built-in optimize will try to cache views

<?php

use Illuminate\Support\Facades\Artisan;

Artisan::command('app:optimize', function () {
    $this->call('app:clear');
    $this->call('config:cache');
    $this->call('event:cache');
    $this->call('route:cache');
});

Artisan::command('app:clear', function () {
    $this->call('cache:clear');
    $this->call('clear-compiled');
    $this->call('config:clear');
    $this->call('event:clear');
    $this->call('route:clear');
});

Please or to participate in this conversation.