Ad

Laravel Routing: Where Is The Prefix() Method Defined?

In my example, I defined a route:

Route::prefix('home')->group(function(){
    Route::get('/test',....);
});

When I access mydomain/home/test, it works. However, I don't know where the prefix method is located. I think it might be in Illuminate\Routing\Route.php. But when I remove the prefix method on that, is it still working?

Ad

Answer

@louisfischer answer is incorrect. The Router::prefix is not called at all.

The prefix method is called from RouteRegistrar, which is finally forwarded to the RouteRegistrar::attribute method.

You can verify this by looking in the Illuminate\Support\Facades\Route docblock.

* @method static \Illuminate\Routing\RouteRegistrar prefix(string  $prefix)

Here is the complete flow:

  1. The Route facade first forwards the call to Illuminate\Routing\Router through Facade::__callStatic.
  2. Since Router::prefix is not a public method but a protected method, this call is forwarded to RouteRegistrar::attribute through the Router::__call method.
  3. Finally the prefix($url) method is converted to RouteRegistrar::attribute('prefix', $url)
Ad
source: stackoverflow.com
Ad