dongluan1743 2014-06-13 13:00
浏览 20
已采纳

如何在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?

展开全部

  • 写回答

1条回答 默认 最新

  • duanbing8817 2014-06-13 13:22
    关注

    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.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部