dongluan1743 2014-06-13 21: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 21: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.

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

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启