提问者:小点点

Laravel 5.6-404,尽管路线已定义


试图访问admin/users/multidel,这给了我一个404错误

镀铬控制台。日志:

DELETE http://****.test/admin/users/multidel 404 (Not Found)

网状物php

Route::group(['middleware'=>'admin'], function(){
    Route::get('/admin', 'AdminController@index')->name('admin.home');;

    Route::delete('admin/users/multidel', 'AdminUsersController@multiDel')->name('users.multidel');
    Route::resource('admin/users', 'AdminUsersController');
});

php artisan路由:列表

+--------+-----------+-------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method    | URI                     | Name             | Action                                                                 | Middleware   |
+--------+-----------+-------------------------+------------------+------------------------------------------------------------------------+--------------+
|        | GET|HEAD  | /                       |                  | App\Http\Controllers\HomeController@index                              | web,auth     |
|        | GET|HEAD  | admin                   | admin.home       | App\Http\Controllers\AdminController@index                             | web,admin    |
|        | POST      | admin/users             | users.store      | App\Http\Controllers\AdminUsersController@store                        | web,admin    |
|        | GET|HEAD  | admin/users             | users.index      | App\Http\Controllers\AdminUsersController@index                        | web,admin    |
|        | GET|HEAD  | admin/users/create      | users.create     | App\Http\Controllers\AdminUsersController@create                       | web,admin    |
|        | DELETE    | admin/users/multidel    | users.multidel   | App\Http\Controllers\AdminUsersController@multiDel                     | web,admin    |
|        | PUT|PATCH | admin/users/{user}      | users.update     | App\Http\Controllers\AdminUsersController@update                       | web,admin    |
|        | GET|HEAD  | admin/users/{user}      | users.show       | App\Http\Controllers\AdminUsersController@show                         | web,admin    |
|        | DELETE    | admin/users/{user}      | users.destroy    | App\Http\Controllers\AdminUsersController@destroy                      | web,admin    |
|        | GET|HEAD  | admin/users/{user}/edit | users.edit       | App\Http\Controllers\AdminUsersController@edit                         | web,admin    |
|        | GET|HEAD  | api/user                |                  | Closure                                                                | api,auth:api |
|        | GET|HEAD  | home                    | home             | App\Http\Controllers\HomeController@index                              | web,auth     |
|        | GET|HEAD  | login                   | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
|        | POST      | login                   |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
|        | POST      | logout                  | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web          |
|        | POST      | password/email          | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest    |
|        | POST      | password/reset          |                  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest    |
|        | GET|HEAD  | password/reset          | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest    |
|        | GET|HEAD  | password/reset/{token}  | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest    |
|        | POST      | register                |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
|        | GET|HEAD  | register                | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
+--------+-----------+-------------------------+------------------+------------------------------------------------------------------------+--------------+

我在web.php中定义了它,它在资源(用户)之上,所以应该优先于“用户”资源中的任何其他路由。我错过了什么?

//编辑

视图代码:

                                            <li class="m-nav__item">
                                                <a href="{{route('users.multidel')}}" data-method="delete" class="m-nav__link" id="multi-delete">
                                                    <i class="m-nav__link-icon la la-trash-o"></i>
                                                    <span class="m-nav__link-text">
                                                        Multi-Delete
                                                    </span>
                                                </a>
                                            </li>

以及驱动它的js:

        $("#multi-delete").on("click", function(e) {
            e.preventDefault();
            var $this = $(this);

            var selected_ids = [];
            var selected_hids = "";
            $("tbody .m-checkable:checked").each(function() {
                selected_ids.push($(this).val());
                selected_hids += $("#data-row-"+$(this).val()).find("a.jquery-postback").data('hid')+"<br>";
            });

            swal({
                title: "Are you sure?",html: "Are you sure you want to delete <strong>multiple records</strong>?<br>The following will be deleted:<br>"+selected_hids+"<br>You won't be able to revert this action once taken!",type: "warning",showCancelButton: true,confirmButtonText: "Yes, delete all records!"
            }).then(function(ev) {
                if (ev.value) {
                    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});

                    $.post({
                        type: $this.data('method'),
                        url: $this.attr('href'),
                        data: selected_ids,
                    }).done(function (result) {
                        console.log($result);
                        if (result.status == 1) {
                            bootstrapNotify('success', 'Deleted Successfully', 'Your records "' + $this.data('hid') + '" was deleted successfully.');
                            $('#records-table').DataTable().row("#data-row-" + result.id).remove().draw();
                        }
                        else {
                            bootstrapNotify('danger', 'Delete Failed', 'Your record "' + $this.data('hid') + '" was <strong>not</strong> deleted successfully.');
                        }
                    });
                }
            })
        });

控制器代码:

public function multiDel($request)
{
    $users = User::findOrFail($request->selected_hids);

    $result = 1;
/*
    foreach ($users as $user){
        if (!$user->delete() && $result == 1)
            $result = 0;
    }
*/
    foreach ($users as $user){
        $result += $user->email;
    }
    return response()->json($result);
}

共2个答案

匿名用户

将其添加到资源后面。。

Route::group(['middleware'=>'admin'], function(){
    Route::get('/admin', 'AdminController@index')->name('admin.home');;

    Route::resource('admin/users', 'AdminUsersController');
    Route::delete('admin/users/multidel', 'AdminUsersController@multiDel')->name('users.multidel');
});

匿名用户

我把delete路由放回资源路由之上,并在这一过程中明显地改变了其他一些内容,因为现在它不再是404错误,而是500错误。

"message": "Too few arguments to function App\\Http\\Controllers\\AdminUsersController::multiDel(), 0 passed and exactly 1 expected",

这是一个完全不同的问题,所以我认为原来的查询解决。我想我改变的是函数定义:

public function multiDel($request)

那是。。。multiDel(Request$Request)在一个阶段-我把它放回到那个阶段,然后看到404回来了。进一步调查显示:

message": "No query results for model [App\\User].",

这表明$request数据有问题。奇怪的是,它在加载页面时抛出404,但有一个错误。希望这对将来的人有帮助。。。