dsij89625 2014-10-19 15:00
浏览 42
已采纳

Laravel 4只有根路由工作 - 其他路由返回'未找到控制器方法'

I've got laravel set up on a domain on a linux host and I have a WAMP local host set up.

The only route that works is the root, when ever I try go to another route such as domain.com/account I get a "Controller method not found." error.

In my routes.php file I have:

Route::controller('','LoginController');
Route::controller('account', 'AccountController');

In my LoginController, I have just two methods. getIndex and postIndex.

After a couple of hours Googling with no results and playing around with the routes file amongst things, still nothing worked.

I tried adding the below route which didn't work either.

Route::any('hello', function(){
    return 'hello!';
});

However, I then commented out my Route::controller('','LoginController'); line and the other routes started working!

I then changed it to Route::controller('login','LoginController'); and this and the other routes still worked. I then changed it to Route::any('','LoginController@getIndex'); and the root and other routes still worked. However, doing it this way, when I cliked the login button on my page nothing happened.

So my question really is, is there something wrong with doing Route::controller('','LoginController');? As everything else seems to 'work'

  • 写回答

2条回答 默认 最新

  • douhui9631 2014-10-19 16:12
    关注

    Laravel save an internal collection of registered routes in the $routes member of the Router class. When dispatching a request, a process of picking each element from this collection and test with current request will be executed to find out which route will be handle. This process is affected by the order of your route registering statements.

    When testing each route with the current request, the picked route will be compiled and have a regex pattern. This pattern will be use to check with the current URI by the preg_match function as you can see at this line in Laravel source.

    When using Route::controller a special route will be add to your routes collection. If your input is Route::controller($uri, $controller) then this special routes will have a regex pattern as ^/$uri/?P<_missing>(.*)$ and it tells Laravel that this request belong to a missing method of the $controller controller class.

    In your case, you have set the value of $uri to an empty string which cause the regex of the special route to be ^/?P<_missing>(.*)$ (setting $uri with the string / cause the same effect). Well, this regex will match every URI. So, the internal route looking up process will abort when look to this special route. This is the reason while the exception has been thrown.

    You should not use an empty string or the/ string when register with the Route::controller method like the way you did. Instead, use Route::resource or explicit calls (Route::get, Route::post, ...) to handle your top level routes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别