【问题标题】:Why the list of routes is bigger than the routes declared into the routes/web.php file?为什么路由列表比在 routes/web.php 文件中声明的路由大?
【发布时间】:2017-07-21 04:30:16
【问题描述】:

我对 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');

【问题讨论】:

    标签: php laravel laravel-5 laravel-routing


    【解决方案1】:

    是的,Route::resource 会自动创建一组路由。

    https://laravel.com/docs/5.4/controllers#resource-controllers

    Laravel 资源路由通过一行代码将典型的“CRUD”路由分配给控制器。例如,您可能希望创建一个控制器来处理对应用程序存储的“照片”的所有 HTTP 请求。使用 make:controller Artisan 命令,我们可以快速创建这样一个控制器。

    【讨论】:

      【解决方案2】:

      当您创建资源路由时会发生这种情况。通过这种方式,Laravel 用一行代码将典型的“CRUD”路由分配给控制器。

      更多信息:https://laravel.com/docs/5.4/controllers#resource-controllers

      【讨论】:

        猜你喜欢
        • 2018-12-22
        • 1970-01-01
        • 2020-04-27
        • 1970-01-01
        • 2019-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多