首页 文章

Laravel:[App \ Http \ Controllers \ admin \ PostsController]上不存在方法[索引]

提问于
浏览
0

这个答案没有解决我的问题[Route::controllers - Method [index] does not exist on App\Http\Controllers

在web.php中

Route::prefix('admin')->group(function () {
    Route::resource('post', 'admin\PostsController');
});

在app / Http / Controllers / admin中

我有PostsController.php

其中包含

<?php

namespace App\Http\Controllers\admin;

use App\Model\Post;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $posts = \App\Post::all();
        return view('admin.posts',['posts'=>$posts]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //
        $post = new Post;

        $post->content = $request->input('descr');

        $post->save();
    }

当我去这个网址http://127.0.0.1:8000/admin/post

我收到以下错误

BadMethodCallException [App \ Http \ Controllers \ admin \ PostsController]上不存在方法[index] .

call_user_func_array
…
/vendor/laravel/framework/src/Illuminate/Routing/Controller.php 56 

 public function callAction($method, $parameters)
    {
        print_r($this);
        echo $method;die();
        return call_user_func_array([$this, $method], $parameters);
    }

//输出

App\Http\Controllers\admin\PostsController Object ( [middleware:protected] => Array ( ) ) index

php artisan打印以下内容

php artisan route:list 

|        | GET|HEAD  | admin/post             | post.index   | App\Http\Controllers\admin\PostsController@index   | web

1 回答

  • 1

    使用laravel版本5.4,我运行了这个命令

    php artisan make:controller admin/PostsController --resource

    在web.php路由文件中添加了这个

    Route::prefix('admin')->group(function () { Route::resource('post', 'admin\PostsController'); });

    控制器文件

    namespace App\Http\Controllers\admin;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    
    class PostsController extends Controller
    {
     /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
      public function index()
      {
        echo "call to index";
      }
      ........
       other functions
    
    }
    

    对我来说很好

    enter image description here

相关问题