Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add webPrefix support to the withRouting method #50324

Closed
wants to merge 1 commit into from

Conversation

Israel5
Copy link

@Israel5 Israel5 commented Mar 1, 2024

Overview

This Pull Request proposes the addition of the webPrefix parameter to the withRouting method.

Similarly to the apiPrefix parameter, the webPrefix parameter allows the user to specify a prefix for the web routes, if needed.

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        apiPrefix: '',
        webPrefix: 'content',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

Changes Proposed:

  1. A new parameter named $webPrefix has been added to the withRouting() method signature.
  2. The buildRoutingCallback() method has been modified to accept the $webPrefix parameter and use it to prefix web routes.
  3. The documentation has been updated to reflect the new parameter and its usage.

Why is this change important?

Laravel 11 currently provides a way to specify a prefix for API routes using the apiPrefix parameter. However, there is no 'fluent' way to prefix the web routes.

With this change, users will be able to specify a prefix for web routes if needed. This will avoid the manual addition of prefixes in the web.php file, which in my opinion, would not be the best way to handle this.

By adding the webPrefix parameter, web routes can be prefixed in a similar way to API routes, resulting in more consistent code.

Use Cases

Case 1

When the user wants to group all web routes under a specific URL path, they can prefix the web routes with a specific string.

Case 2

From my own case, I have a Laravel application that is used as an API subdomain. However, I do not use the /api prefix for the API routes. Instead, I leave it blank or use it with something else. Consider the URL like api.example.com/users.

Additionally, I serve a few web routes in my application, and all these routes are grouped under the /content prefix. So, if I wanted to serve a web route for a file called "my-file.pdf", the URL would be api.example.com/content/my-file.pdf.

By making these changes to my application, I can use the /content prefix for my web routes and organize it the same way as my API routes.

Additional Context

As the webPrefix parameter in the withRouting method is set to blank (empty string), it shouldn't change or break any existing code.

Lastly, I believe that the webPrefix is less likely to be used, so it was set after the apiPrefix in the withRouting method signature.

@taylorotwell
Copy link
Member

I don't think it's very common to prefix all web routes? Regardless, you could just use a Route::group to achieve this.

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.

2 participants