3

I started my project using Laravel, but I don't know how routing works.

Example code:

Route::get('/', function () {
    return view('welcome');
});

Where is the get static function? I searched in the Laravel /vendor directory but found nothing.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
DeLe
  • 2,442
  • 19
  • 88
  • 133
  • 1
    You should use https://github.com/barryvdh/laravel-ide-helper to generate an IDE helper file that allows you to get type hints in your IDE and also contains some information on where functions are from – apokryfos Nov 25 '18 at 13:56

2 Answers2

4

Laravel routes are very simple, they keep your project neatly organized. The routes are usually the best place to look to understand an application is linked together.

The Laravel documentation on routing is very elaborate.

The example you sited is an example of a GET route to the / URL. It accepts a callback as a second parameter. This callback determines how the request is processed. In this case, a view response is returned.

Route::get('/', function () {
    return view('welcome');
});

There are different types of routes:

Route::get($uri, $callback);

Route::post($uri, $callback);

Route::put($uri, $callback);

Route::patch($uri, $callback);

Route::delete($uri, $callback);

Route::options($uri, $callback);

You can also pass parameters through your routes:

You may define as many route parameters as required by your route:

Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
     // });

Sometimes you may need to register a route that responds to multiple HTTP verbs. You may do so using the match method. Or, you may even register a route that responds to all HTTP verbs using the any method:

Route::match(['get', 'post'], '/', function () {
    //
});

Route::any('foo', function () {
    //
});

Here is a good piece on the subject.

Elisha Senoo
  • 3,489
  • 2
  • 22
  • 29
4

Actually, you are using Route Facade. Which facilitates to access object members in static environment. Facades uses __callStatic magic method of PHP.

Study the Facades here.

Jamal Abdul Nasir
  • 2,557
  • 5
  • 28
  • 47