欢迎来到 Laravel 的可疑魔法。这些依赖注入的基本思想是,根据你定义路由和控制器的方式,Laravel 可以执行一些 url 的自动解析、这些 url 中的 id 识别以及对象的数据库获取。
我的问题是我不明白 $user 来自哪里。
您可能应该阅读docs on the service container。您还可以使用以下命令更好地了解您的路由定义如何转换为带有参数的 url:
php artisan route:list
在我的一个项目中,结果如下:
+--------+-----------+----------------------------+--------------------+-------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+----------------------------+--------------------+-------------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | categories | categories.index | App\Http\Controllers\CategoryController@index | web |
| | POST | categories | categories.store | App\Http\Controllers\CategoryController@store | web |
| | GET|HEAD | categories/create | categories.create | App\Http\Controllers\CategoryController@create | web |
| | GET|HEAD | categories/{category} | categories.show | App\Http\Controllers\CategoryController@show | web |
| | PUT|PATCH | categories/{category} | categories.update | App\Http\Controllers\CategoryController@update | web |
| | DELETE | categories/{category} | categories.destroy | App\Http\Controllers\CategoryController@destroy | web |
| | GET|HEAD | categories/{category}/edit | categories.edit | App\Http\Controllers\CategoryController@edit | web |
| | GET|HEAD | products | products.index | App\Http\Controllers\ProductController@index | web |
| | POST | products | products.store | App\Http\Controllers\ProductController@store | web |
| | GET|HEAD | products/create | products.create | App\Http\Controllers\ProductController@create | web |
| | GET|HEAD | products/{product} | products.show | App\Http\Controllers\ProductController@show | web |
| | PUT|PATCH | products/{product} | products.update | App\Http\Controllers\ProductController@update | web |
| | DELETE | products/{product} | products.destroy | App\Http\Controllers\ProductController@destroy | web |
| | GET|HEAD | products/{product}/edit | products.edit | App\Http\Controllers\ProductController@edit | web |
+--------+-----------+----------------------------+--------------------+-------------------------------------------------+--------------+
所有这些路由及其 uri 和参数仅由几个非常简单的路由定义生成。这是我的路线文件:
$ cat routes/web.php
<?php
Route::get('/', function () {
return view('master');
});
Route::resource('products', 'ProductController');
Route::resource('categories', 'CategoryController');
如果您查看上述路由输出中的 URI 列表,您会看到在 URI 中命名的参数,例如 {category} 和 {product}。这些对应于 Laravel 标识的 URI 中的 ids/keys。 Laravel 足够“聪明”,可以查看我的 Controller 文件,查看各种函数中的类型提示,并检测到我的函数需要注入依赖项。
例如,Category 控制器的 show 方法如下所示:
public function show(Tree $category)
{
var_dump($category);
}
我的控制器可能看起来有点不寻常,因为我在输入提示我想要一个 Tree 类型的对象,但是 Laravel 足够聪明地认识到我确实想要一个 Tree 类型的模型,所以它解析出 url并在其中找到 id 并自动获取我的数据库表 trees 中的记录,其中 id 与我的 url 的 {category} 片段匹配,并将其注入我的函数中。
请注意,当我尝试将输入参数命名为 $tree 而不是 $category 时,我 had some trouble。其他线程也可能有助于回答您的问题。
最重要的是,Laravel 做了很多“魔法”,希望您能从繁琐的手动定义自己的代码和查询以检索您想要的对象中解脱出来。