DeLe
DeLe

Reputation: 2480

Understanding Routing in Laravel

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.

Upvotes: 3

Views: 341

Answers (2)

Elisha Senoo
Elisha Senoo

Reputation: 3594

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.

Upvotes: 4

Jamal Abdul Nasir
Jamal Abdul Nasir

Reputation: 2667

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.

Upvotes: 4

Related Questions