我正在运行PHP 7.0.25
和Laravel Framework 5.5.25
。
我想使用laravel datatables package并跟随quickstart guide并且具有完全相同的controller
和view
。
但是,在运行我的应用程序时,我收到以下错误:
BadMethodCallException thrown with message "Method [index] does not exist on [App\Http\Controllers\DatatablesController]."
Stacktrace:
#51 BadMethodCallException in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:68
#50 Illuminate\Routing\Controller:__call in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
#49 call_user_func_array in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
#48 Illuminate\Routing\Controller:callAction in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45
#47 Illuminate\Routing\ControllerDispatcher:dispatch in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Route.php:212
#46 Illuminate\Routing\Route:runController in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Route.php:169
#45 Illuminate\Routing\Route:run in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Router.php:658
#44 Illuminate\Routing\Router:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
#43 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
#42 Illuminate\Routing\Middleware\SubstituteBindings:handle in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#41 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#40 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:67
#39 Illuminate\Foundation\Http\Middleware\VerifyCsrfToken:handle in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#38 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#37 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php:49
#36 Illuminate\View\Middleware\ShareErrorsFromSession:handle in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#35 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#34 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:63
#33 Illuminate\Session\Middleware\StartSession:handle in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#32 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#31 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php:37
#30 Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse:handle in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#29 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#28 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php:59
#27 Illuminate\Cookie\Middleware\EncryptCookies:handle in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#26 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#25 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102
#24 Illuminate\Pipeline\Pipeline:then in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Router.php:660
#23 Illuminate\Routing\Router:runRouteWithinStack in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Router.php:635
#22 Illuminate\Routing\Router:runRoute in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Router.php:601
#21 Illuminate\Routing\Router:dispatchToRoute in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Router.php:590
#20 Illuminate\Routing\Router:dispatch in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:176
#19 Illuminate\Foundation\Http\Kernel:Illuminate\Foundation\Http\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
#18 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/fideloper/proxy/src/TrustProxies.php:56
#17 Fideloper\Proxy\TrustProxies:handle in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#16 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#15 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:30
#14 Illuminate\Foundation\Http\Middleware\TransformsRequest:handle in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#13 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#12 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:30
#11 Illuminate\Foundation\Http\Middleware\TransformsRequest:handle in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#10 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#9 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
#8 Illuminate\Foundation\Http\Middleware\ValidatePostSize:handle in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#7 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#6 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:46
#5 Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode:handle in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#4 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#3 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102
#2 Illuminate\Pipeline\Pipeline:then in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:151
#1 Illuminate\Foundation\Http\Kernel:sendRequestThroughRouter in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116
#0 Illuminate\Foundation\Http\Kernel:handle in /home/ubuntu/workspace/public/index.php:55
我希望这个错误来自我的路由配置,我已将其更改为Route::resource
,如quickstart guide中所述,已弃用 - 因为laravel 5.3 - Route::controllers
:
Route::resource('datatables', 'DatatablesController', [
'anyData' => 'datatables.data',
'getIndex' => 'datatables',
]);
有关如何正确编写路线的任何建议吗?
PS:我已经使用过composer dumpautoload
,composer install
& composer update
并且它不会更改错误
更新
我当前的控制器如下所示:
namespace App\Http\Controllers;
use App\Http\Requests;
use App\User;
use Yajra\Datatables\Datatables;
class DatatablesController extends Controller
{
/**
* Displays datatables front end view
*
* @return \Illuminate\View\View
*/
public function getIndex()
{
return view('datatables.index');
}
/**
* Process datatables ajax request.
*
* @return \Illuminate\Http\JsonResponse
*/
public function anyData()
{
return Datatables::of(User::query())->make(true);
}
}
我的观点:
@extends('layouts.master')
@section('content')
<table class="table table-bordered" id="users-table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Created At</th>
<th>Updated At</th>
</tr>
</thead>
</table>
@stop
@push('scripts')
<script>
$(function() {
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: '{!! route('datatables.data') !!}',
columns: [
{ data: 'id', name: 'id' },
{ data: 'name', name: 'name' },
{ data: 'email', name: 'email' },
{ data: 'created_at', name: 'created_at' },
{ data: 'updated_at', name: 'updated_at' }
]
});
});
</script>
@endpush
答案 0 :(得分:1)
答案 1 :(得分:0)
在routes.php中,Laravel搜索控制器(在您的情况下为DatatablesController
)以及在此控制器中执行的方法,如DatatablesController@methodToExecute
。
默认情况下,Laravel尝试执行方法index
。就像@madalinivascu所写,这个控制器需要存在。