如何在Laravel(IoC容器)中解决BadMethodCallExeption

When I add Route::resource('api', 'ApiController'); to my app/Http/routes.php, I get this error:

BadMethodCallException Method [index] does not exist.

My api controller looks like this:

<?php

    class ApiController extends BaseController
    {     
        public function getIndex()
        {
           echo "No Access";
        }
        public function postIndex()
        {
            // content here ...
        }
        public function check_ban($user, $gameBit, $ip)
        {
            // content here ...
        }
        public function check_balance($userid, $gameid, $serverid)
        {
            // content here ...
        }
        public function purchase_item($xml)
        {
            // content here ...
        }
        public function LoginRequest($xml)
        {
            // content here ...
        }
        public function CurrencyRequest($xml)
        {
            // content here ...
        }
        public function ItemPurchaseRequest($xml)
        {
            // content here ...
        }
    }

My api should handle out game login request, buying items, ban check and so on, but I get the error described above.

How do I resolve it please?

When you are using following to declare a resourceful route

Route::resource('api', 'ApiController');

Then let Laravel generate methods for you so go to command prompt/terminal and run:

php artisan controller:make ApiController

This will create a ApiController resourceful controller in app/controllers directory and all the methods will be (Method's skeleton) created. To make sure which method listens to which URI and HTTP method, run php artisan routes from command prompt/terminal so you'll get the idea. Read more about resourceful controllers on Laravel website.

If you want to declare your own methods against HTTP verbs then create a RESTful controller and to create a RESTful controller you don't have to run any artisan command from terminal but declare the route using:

Route::controller('api', 'ApiController');

Then create methods prefixing with HTTP verbs for responding to that verb for example:

class ApiController extends BaseController {

    // URL: domain.com/api using GET HTTP method
    public function getIndex()
    {
        //...
    }

    // URL: domain.com/api/profile using POST HTTP method
    public function postProfile()
    {
        //...
    }

    // ...
}

If you run php artisan routes then you will be able to see all the URIs and method mapping. Check RESTful controllers on Laravel website.