0

This answer doesn't solve my problem Route::controllers - Method [index] does not exist on [App\Http\Controllers

In web.php

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

in app/Http/Controllers/admin

I have PostsController.php

which contains

<?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();
    }

When I go to this url http://127.0.0.1:8000/admin/post

I get the following error

BadMethodCallException Method [index] does not exist on [App\Http\Controllers\admin\PostsController].

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);
    }

//prints

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

php artisan prints the following

php artisan route:list 

|        | GET|HEAD  | admin/post             | post.index   | App\Http\Controllers\admin\PostsController@index   | web
11
  • Try to access posts. Commented Feb 12, 2018 at 18:42
  • @MASh Dou want me to try 127.0.0.1:8000/admin/posts instead of 127.0.0.1:8000/admin/post I get page not found error Commented Feb 12, 2018 at 18:43
  • yes. try this one. Commented Feb 12, 2018 at 18:44
  • @MASh I get page not found error Commented Feb 12, 2018 at 18:45
  • 1
    Then there is something. Write a route: Route:get('posts','admin\PostsController@index'); and now try to access to admin/posts`. Commented Feb 12, 2018 at 18:49

2 Answers 2

2

Maybe you have Ambiguous class in your controllers, that means you may have same class name in different controllers. To solve that problem, run these commands

 1. composer update 
 2. composer dumpautoload
 3. php artisan config:cache
 4. php artisan view:clear

Make sure that composer update runs successfully without errors. If errors are mentioned in the console, correct all of them where they occur in your codebase.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks chap! U made my day, apparently, i spent a few hrs debugging what was wrong with my routes and controller, finally caught that i had renamed a controller to ControllerOld but the Controller class name still existed in the old controller hence the Controller was conficting with class name defined in old controller hence this exception! Happy coding
1

Using laravel version 5.4 , i ran this commands

php artisan make:controller admin/PostsController --resource

in web.php routes file added this

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

controller file

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

}

works fine for me

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.