Configurable Generation of Web & API Routes #2129
Open
+65
−16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Feature: Configurable Generation of Web & API Routes
Overview
This PR addresses issue #2110 by enabling developers to control whether web and API route files and methods are generated in each module's
RouteServiceProvider. In previous versions, methods for web and API routes were always included—even if the corresponding route files were missing—which caused errors.Problem Statement
Previously, even when developers disabled generation of
routes/web.phporroutes/api.php(by adjusting configuration), theRouteServiceProviderwould still contain the methodsmapWebRoutes()and/ormapApiRoutes(). These would reference missing files, causing runtime errors.Solution Summary
This PR introduces more granular configuration and stub-processing so that:
RouteServiceProviderwill only include code for enabled route types, avoiding references to files that don't exist.Breakdown of the Approach
1. Configuration Improvements
webandapiundermodules.paths.generator.routesin the config.2. Conditional Code Generation
RouteProviderMakeCommandchecks these configuration settings.mapWebRoutes()and/ormapApiRoutes()methods in the generated file if corresponding flags aretrue.3. Stub Template Enhancements
%START_WEB_ROUTES%...%END_WEB_ROUTES%and%START_API_ROUTES%...%END_API_ROUTES%) have been added to the stub template.4. Enhanced Stub Processing
Stubclass now:How Removal Tag Markers Work (Contributor Docs)
What are they?
Syntax:
%START_TAG_NAME%%END_TAG_NAME%When you specify a tag (e.g.,
WEB_ROUTES) for removal, all code between its start and end markers is removed from the generated file.Implementation Internals
Stubclass includes:$removalTags— array of markers to remove.setRemovalTags(array $tagsArray)— specify markers to remove.removeContentsBetweenTagMarkers(string $tag, string $contents)— removes code wrapped in that tag, via regex.cleanUpTagMarkers(string $contents)— removes any tags left over.Example Usage
In a stub (
route-provider.stub):In the generation command:
Result:
If
'web' => falsein config, themapWebRoutes()function will not be present in the provider class.Contributor Benefits
Technical Details
sflag (dot matches newlines):/\%START_TAG%.*?\%END_TAG%/sWhat Was Changed
'web' => true, 'api' => trueto route generation config block.shouldGenerateWebRoutes()andshouldGenerateApiRoutes().getTemplateContents()to use removal tags logic.Configuration Usage
Easily control which routes are generated in
config/modules.php:If
weborapiis set tofalse, the corresponding method will not be present in the generated provider file, avoiding references to missing route files.Testing Performed
mapWebRoutes()is only generated ifweb => true.mapApiRoutes()is only generated ifapi => true.true.false.truefor both) remains fully compatible.Related Issue
Closes #2110