Skip to content

feat: scaffold routes with explicit controller imports - #47

Draft
eaguad1337 wants to merge 1 commit into
5.0from
feat/scaffold-explicit-controller-imports
Draft

feat: scaffold routes with explicit controller imports#47
eaguad1337 wants to merge 1 commit into
5.0from
feat/scaffold-explicit-controller-imports

Conversation

@eaguad1337

@eaguad1337 eaguad1337 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Context

Comes out of discussion #42: the default routes/web.py uses the string syntax "WelcomeController@show", and adopting an explicit controller-import style was proposed.

Finding

This syntax is already supported by the framework — no core changes are required. HTTPRoute._find_controller (src/masonite/routes/HTTPRoute.py:187-202) already resolves a Class.method reference passed directly via __qualname__/__module__. It is in fact the same mechanism Route.view/Route.redirect use internally (ViewController.show, RedirectController.redirect).

So this PR is a scaffold-only, non-breaking change.

Change

Updates the skeleton route files to import the controller class and pass the method directly:

from masonite.routes import Route

from app.controllers.WelcomeController import WelcomeController

ROUTES = [
    Route.get("/", WelcomeController.show).name("welcome"),
]

(The commented example in api.py is updated to match.)

Why

  • Enables go-to-definition, autocomplete and rename refactors in the IDE.
  • Typos / missing controllers fail at import time instead of at runtime with a warning + SyntaxError when the request hits the route.
  • Removes the "magic" string resolution via controllers_locations.

Compatibility

  • The string "Controller@method" form remains fully supported; existing apps are unaffected.
  • The Route.resource() / Route.api() helpers still take a string controller. Standardizing those to the class style would be a separate follow-up (the only point of inconsistency with this convention).

Closes #42.

Switch the default skeleton route files from the string
"WelcomeController@show" reference to importing the controller class and
passing the method directly (WelcomeController.show).

This is already fully supported by HTTPRoute._find_controller (the same
mechanism Route.view/Route.redirect use internally), so it is a non-breaking
scaffold-only change. The explicit style enables go-to-definition,
autocomplete, rename refactors and import-time errors for missing controllers
instead of runtime string resolution.

The string "Controller@method" form remains fully supported, as do the
Route.resource()/Route.api() helpers which still take a string controller.
@eaguad1337
eaguad1337 force-pushed the feat/scaffold-explicit-controller-imports branch from 65fb714 to 4c17523 Compare June 9, 2026 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant