user3665428 user3665428 - 4 months ago 14
PHP Question

How to resolve BadMethodCallExeption in Laravel (IoC Container)

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()
{
public function check_ban($user,$gameBit,$ip)
{
...
public function check_balance($userid, $gameid, $serverid)
...
public function purchase_item($xml)
{
..,
public function LoginRequest($xml)
{
...
public function CurrencyRequest($xml)
{
...
public function ItemPurchaseRequest($xml)
{


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?

Answer

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.

Comments