Validating Passwords and Confirmation Fields in Laravel
When someone creates or resets a password in a Laravel form, the server should check that the password is present, meets the…
In Laravel, route files connect URLs to the code that handles requests. The file where you register a route also determines which default rules Laravel applies. Laravel calls these rules middleware. Middleware runs as Laravel handles a request; it can manage browser sessions or protect against forged requests.
Use routes/web.php for routes that need browser behavior, routes/api.php for API routes, and custom files when a distinct group of routes benefits from its own organization. A filename alone does not set a route’s behavior. Laravel’s route configuration determines which files it loads and which middleware and URL prefixes it applies.
web.php and api.php provideLaravel uses routes/web.php for its browser-facing routes. Routes in that file receive the web middleware group, which provides features such as session handling and protection against cross-site request forgery (CSRF), a type of attack that tricks a browser into sending an unwanted request. Page and form routes commonly belong here. The file determines a web route’s default middleware, and the route can still return JSON. Laravel documents the web route behavior and the middleware groups.
Laravel 13 does not include API routing in every new project by default. Run php artisan install:api to install Sanctum and create routes/api.php. Laravel then gives routes in that file the api middleware group and, by default, adds /api to their URLs. A route declared as /users therefore appears at /api/users. The prefix can be changed in bootstrap/app.php, where Laravel configures route loading. See the API routing setup.
The api middleware group does not provide all the security a route needs. Putting a route in api.php does not automatically set up authentication, which checks who is making a request; authorization, which checks what they are allowed to do; or rate limits, which restrict how many requests they can make. Add the middleware that provides these protections. Laravel’s middleware documentation describes the default groups and how applications configure them.
Create a custom file when it makes a related group of routes easier to organize, such as webhook endpoints or many routes for one feature. Laravel does not give a file such as routes/webhooks.php special behavior because of its name. You must register the file and choose its middleware, URL prefix, and route-name prefix.
In Laravel 11 and later application structures, bootstrap/app.php configures route loading through withRouting. Its then option lets you register additional routes while keeping the normal web and API route setup. For example:
use Illuminate\Support\Facades\Route;
->withRouting(
web: __DIR__.'/../routes/web.php',
then: function () {
Route::middleware('api')
->prefix('webhooks')
->name('webhooks.')
->group(base_path('routes/webhooks.php'));
},
)
Here, the custom routes use the api middleware group. Their URLs start with /webhooks, and their route names start with webhooks.. A route group applies shared settings to multiple routes, so each route does not need to repeat those settings. Laravel documents this approach in its custom route registration guidance.
With the using option, you register HTTP route files yourself. Choose it only when you intend to register every required file, including the usual web and API files if you still need them.
Keep browser routes in web.php when they need the web middleware group. Put API endpoints in api.php when they belong to your API and should use its middleware and URL-prefix conventions. Add a custom file when it makes a clearly defined group of routes easier to find, then assign the middleware and URL settings that group needs.
This approach avoids splitting routes simply to reduce the number of lines in one file. Laravel sets no official threshold for when a project should add another route file, and its documentation does not claim that any particular file arrangement improves maintainability. Make the split serve a recognizable boundary, such as different middleware or a distinct URL area.
Project structure depends on the Laravel version. Laravel 10 applications commonly register route files through App\Providers\RouteServiceProvider; Laravel 11 introduced a streamlined structure that configures routing in bootstrap/app.php. New Laravel 11 projects also stopped including api.php by default. Check your project’s bootstrap/app.php or RouteServiceProvider before copying a configuration example. The Laravel 11 release notes describe the newer structure, while the Laravel 10 middleware documentation shows the older provider-based setup.
After you change route registration or middleware groups, run php artisan route:list to see which routes Laravel recognizes. Add -v or -vv to show middleware, or use --path=api to filter routes by URL text. Check each route’s URL and middleware to confirm that Laravel loaded the intended file and applied the expected settings.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com