我是 Laravel 的新手,我有以下疑问。
如果我执行 php artisan route:list 语句以获取Laravel网站上所有路线的列表,我会获得此输出:
$ php artisan route:list
+--------+-----------+----------------------------------+----------------------+-----------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+----------------------------------+----------------------+-----------------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | POST | contact | | App\Http\Controllers\EnquiryController@index | web |
| | GET|HEAD | contact | | Closure | web |
| | POST | registration | registration.store | App\Http\Controllers\RegistrationController@store | web |
| | GET|HEAD | registration | registration.index | App\Http\Controllers\RegistrationController@index | web |
| | GET|HEAD | registration/create | registration.create | App\Http\Controllers\RegistrationController@create | web |
| | PUT|PATCH | registration/{registration} | registration.update | App\Http\Controllers\RegistrationController@update | web |
| | GET|HEAD | registration/{registration} | registration.show | App\Http\Controllers\RegistrationController@show | web |
| | DELETE | registration/{registration} | registration.destroy | App\Http\Controllers\RegistrationController@destroy | web |
| | GET|HEAD | registration/{registration}/edit | registration.edit | App\Http\Controllers\RegistrationController@edit | web |
+--------+-----------+----------------------------------+----------------------+-----------------------------------------------------+--------------+
但是进入 routes / web.php 文件我只声明了这些内容:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('contact', function() {
return View::make('contact');
});
Route::post('contact', 'EnquiryController@index');
Route::resource('/registration', 'RegistrationController');
因此,有效路线的数量似乎大于路线有效地声明为 routes / web.php 文件。
为什么呢?也许它可能取决于这个具体的定义?
Route::resource('/registration', 'RegistrationController');
答案 0 :(得分:2)
是的,Route::resource
会自动创建一组路线。
https://laravel.com/docs/5.4/controllers#resource-controllers
Laravel资源路由分配典型的&#34; CRUD&#34;使用单行代码路由到控制器。例如,您可能希望创建一个控制器来处理&#34;照片&#34;的所有HTTP请求。由您的应用程序存储。使用make:controller Artisan命令,我们可以快速创建这样的控制器。
答案 1 :(得分:1)
创建资源路径时会发生这种情况。通过这种方式,Laravel分配了典型的&#34; CRUD&#34;使用单行代码路由到控制器。
更多信息:https://laravel.com/docs/5.4/controllers#resource-controllers