了解 Laravel 中的路由

Understanding Routing in Laravel

我使用 Laravel 开始了我的项目,但我不知道路由是如何工作的。

示例代码:

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

get 静态函数在哪里?我在 Laravel /vendor 目录中搜索但没有找到。

Laravel 路线非常简单,它们使您的项目井井有条。路由通常是了解应用程序链接在一起的最佳位置。

Laravel documentation on routing讲的很详细

您选择的示例是 / URL 的 GET 路由示例。 它接受回调作为第二个参数。此回调确定如何处理请求。在这种情况下,将返回一个视图响应。

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

有不同类型的路线:

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 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.

实际上,您正在使用 Route Facade。这有助于在静态环境中访问对象成员。 Facades使用了__callStaticPHP的魔法方法。

研究 Facades here