Skip to content

Laravel without frontend

I've recently started working on a new project where Laravel is used as a backend API only. It was always possible to use Laravel without frontend scaffolding, but it recently became a bit more complicated, especially with the new artisan dev command.

Delete the frontend scaffolding

So when you create a new Laravel project, it comes with some frontend scaffolding. You can remove it by deleting the following files and folders:

  • .npmrc
  • package.json
  • resources/js/app.js
  • resources/views/welcome.blade.php
  • vite.config.js

This is if you used the Blade frontend scaffolding. If you used another preset, you might have to delete additional files.

Remove the web routes

This step is optional. By default Laravel ships with a routes/web.php file where you reference your routes by default. This goes through the web middleware and is useful when you serve a frontend (sessions, validations etc..). In my case I'll only deal with API routes so I can safely remove this file.

When it's done, you must remove it from the routing configuration.

bootstrap/app.php

_21
<?php
_21
_21
use Illuminate\Foundation\Application;
_21
use Illuminate\Foundation\Configuration\Exceptions;
_21
use Illuminate\Foundation\Configuration\Middleware;
_21
use Illuminate\Http\Request;
_21
_21
return Application::configure(basePath: dirname(__DIR__))
_21
->withRouting(
_21
- web: __DIR__.'/../routes/web.php',
_21
commands: __DIR__.'/../routes/console.php',
_21
health: '/up',
_21
)
_21
->withMiddleware(function (Middleware $middleware): void {
_21
//
_21
})
_21
->withExceptions(function (Exceptions $exceptions): void {
_21
$exceptions->shouldRenderJsonWhen(
_21
fn (Request $request) => $request->is('api/*') || $request->expectsJson(),
_21
);
_21
})->create();

If you want to setup the api you can do so with artisan install:api

Disable vite from the artisan dev command

By default artisan dev will try to run vite to compile the frontend assets. Since we don't have any frontend scaffolding, we need to disable this behavior.

app/Providers/AppServiceProvider.php

_25
<?php
_25
_25
namespace App\Providers;
_25
_25
use Illuminate\Foundation\DevCommands;
_25
use Illuminate\Support\ServiceProvider;
_25
_25
class AppServiceProvider extends ServiceProvider
_25
{
_25
/**
_25
* Register any application services.
_25
*/
_25
public function register(): void
_25
{
_25
//
_25
}
_25
_25
/**
_25
* Bootstrap any application services.
_25
*/
_25
public function boot(): void
_25
{
_25
+ DevCommands::except('vite'); // Disable the "vite" command as there is not frontend
_25
}
_25
}

Now your app is cleaned from any frontend and you can start working on your API.