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条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配