Route::controllers - [App\Http\Controllers 上不存在方法 [index]

Posted

技术标签:

【中文标题】Route::controllers - [App\\Http\\Controllers 上不存在方法 [index]【英文标题】:Route::controllers - Method [index] does not exist on [App\Http\ControllersRoute::controllers - [App\Http\Controllers 上不存在方法 [index] 【发布时间】:2018-05-25 19:23:20 【问题描述】:

我正在运行php 7.0.25Laravel Framework 5.5.25

我想使用laravel datatables package 并遵循quickstart guide 并拥有完全相同的controllerview

但是,在运行我的应用程序时,我收到以下错误:

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

【问题讨论】:

【参考方案1】:

需要使用 artisan 生成资源控制器

php artisan make:controller DatatablesController --resource

创建控制器将具有您的资源路由所需的方法

more info

注意:第三个参数(数组)无效

【讨论】:

感谢您的回复!我生成了资源控制器。如何,正确的控制器和路线应该是什么样子?恳请您提供示例代码。 控制器看起来像生成的控制器,在路由集合中你应该删除第三个参数,我不知道你在做什么 请看上面我对当前控制器的更新。我基本上想加载索引页面并使用函数anyData加载axjax请求。 如果你有自定义方法,你需要自定义路由 Route::get('datatables/data', 'DatatablesController@getIndex')-&gt;name('datatables.data')【参考方案2】:

在你的 routes.php 中,Laravel 搜索一个控制器(在你的例子中是 DatatablesController)和一个在这个控制器中执行的方法,比如 DatatablesController@methodToExecute。 默认情况下,Laravel 会尝试执行 index 方法。就像@madalinivascu 写的那样,这个控制器需要存在。

【讨论】:

以上是关于Route::controllers - [App\Http\Controllers 上不存在方法 [index]的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5密码重置不起作用

iOS app内跳转其他app

APP运营最基本目标:让用户喜欢你的APP

苹果app上架Appstore网页APP上架 彩票原生app上架全程协助

app是如何制作的,APP如何开发?

APP启动流程及分析APP启动性能的方法!