0

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?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
DeLe
  • 2,442
  • 19
  • 88
  • 133

3 Answers3

0

The prefix method may be used to prefix each route in the group with a given URI.

You can use this code:

Route::group(['prefix' => 'home'], function () {
   Route::get('test', 'yourController@yourFunctions');
}

or this

Route::prefix('home')->group(function () {
    Route::get('test', 'yourController@yourFunctions');
});

It doesn't matter because it gives same results.

More information: https://laravel.com/docs/5.4/routing#route-group-prefixes

Ryuujo
  • 613
  • 1
  • 9
  • 26
  • Hi, I using Laravel 5.4, but i want to know where `prefix` method define? – DeLe Dec 04 '18 at 08:03
  • I drilled down the `prefix` method and it was from `_ide_helper.php` it says like this: `@method static \Illuminate\Routing\RouteRegistrar prefix(string $prefix)` – Ryuujo Dec 04 '18 at 08:06
  • I cannot found `_ide_helper.php` file, and `prefix` method in `\Illuminate\Routing\RouteRegistrar` – DeLe Dec 04 '18 at 08:20
  • The OP is not asking what code to use. He is asking where the `prefix` method is defined and why. – Paras Dec 06 '18 at 12:19
0

The prefix method is located in the Illuminate\Routing\Router class.

The absolute path in a typical Laravel 5 application would be:

vendor/laravel/framework/src/Illuminate/Routing/Router.php

How to find it?

You probably know that when you write Route:: you are actually using the Laravel facade called Route. From there you can find the facade class reference in the official documentation of Laravel 5.4 at https://laravel.com/docs/5.4/facades#facade-class-reference. Here is the line you are looking for:

---------------------------------------------------------------
|Facade |Class                     |Service Container Binding |
---------------------------------------------------------------
|Route  |Illuminate\Routing\Router |router                    |
---------------------------------------------------------------

The flow

  1. The (protected) prefix method is handled by the __call@Router. return (new RouteRegistrar($this))->attribute($method, $parameters[0]); is executed
  2. the attribute@RouteRegistrar method is called and the element 'prefix' => 'home' is added to the $this->attributes array
  3. Since the attribute@RouteRegistrar method returns $this, the group@RouteRegistrar method is called
  4. The group@RouteRegistrar method calls the group@Router method
  5. The loadRoutes@Router method is called. It will parse all routes present in the closure, i.e. Route::get('/test', ...);
  6. This time the get method is called on the class underlying the Route facade, the Router class. The get@Router method is executed
  7. addRoute@Router is called, which in turn calls the createRoute@Router method
  8. createRoute@Router calls newRoute@Router with the prefixed URI as second argument

You will notice that the prefix@Router method calls getLastGroupPrefix@Router which obviously retrieve the prefix of the last group.

louisfischer
  • 1,968
  • 2
  • 20
  • 38
  • But in `Illuminate\Routing\Router` prefix method return `string` ? but i think it must return `class` – DeLe Dec 06 '18 at 09:22
  • I know you may think that it must return the class itself because of the method chaining. But this is made possible thanks to the [`Macroable` trait](https://laravel.com/api/5.4/Illuminate/Support/Traits/Macroable.html). Check [this concise article](https://unnikked.ga/understanding-the-laravel-macroable-trait-dab051f09172) as an introduction on the Laravel `Macroable` trait. – louisfischer Dec 06 '18 at 10:17
  • No that is incorrect. Macroable only works when we call `Route::macro`. In fact the prefix method in the `Router` class is not called on `Route::prefix`. – Paras Dec 06 '18 at 12:08
  • You just changed the meaning of your entire answer now that you've realized that my answer is the correct one and your original answer was incorrect. Wow! Great sportsmanship! – Paras Dec 06 '18 at 14:30
  • I never modified anything, just added things to be clearer. You can check the revisions ;) – louisfischer Dec 06 '18 at 14:32
  • Originally you said `Router::prefix` is called. Now you're saying `RouteRegistrar::attribute` is called. In fact you even argued that Router::prefix is called because of `Macroable`. Yes, your edits and comments are public for everyone to see. – Paras Dec 06 '18 at 14:33
  • I indeed **answered his question in the comments** too quickly. After your comment, I had a better look and added details to my answer. – louisfischer Dec 06 '18 at 14:47
0

@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)
Paras
  • 9,258
  • 31
  • 55
  • `\Illuminate\Routing\RouteRegistrar` is the return type. Check [this Stack Overflow answer](https://stackoverflow.com/a/15634488/4559927) about the `@method` tag. – louisfischer Dec 06 '18 at 13:29
  • That's exactly my point. it is the return type because the `RouteRegistrar::attribute` returns `$this`. Whereas you're suggesting `Router::prefix` whose return type is string not RouteRegistrar. Your answer is wrong. – Paras Dec 06 '18 at 14:22
  • I guess you didn't even read the OP question. He asked where he can find the `prefix` method. My answer is correct and simple: in the `Illuminate\Routing\Router` class – louisfischer Dec 06 '18 at 14:33
  • No, he meant where he can find the `Route::prefix` method that is called. That refers to `RouteRegistrar::attribute`. The `Router::prefix` is not even called on `Route::prefix` and is totally irrelevant to the question. – Paras Dec 06 '18 at 14:34
  • "The `prefix` method is located in the class `Illuminate\Routing\Router`" answers pretty well the question "i don't know where is prefix method?" to me. – louisfischer Dec 06 '18 at 14:40
  • Yes because you didn't read the question. He was referring the `Route::prefix` method. The `Router::prefix` method is not the same as `Route::prefix`. – Paras Dec 06 '18 at 14:50
  • I think you are mixing things up. The `Route` facade references the `Illuminate\Routing\Router` class. – louisfischer Dec 06 '18 at 15:21
  • How does that relate to the question? You're the one who's mixing things up – Paras Dec 06 '18 at 15:25
  • 1
    Thankiu @louisfischer and Paras. This is what i looking for :) – DeLe Dec 06 '18 at 17:21