在 Laravel 4 中找不到控制器路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15588409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:37:54  来源:igfitidea点击:

Controller Route not found in Laravel 4

phproutinglaravellaravel-4

提问by Nyxynyx

I am in the process of migrating from L3 to L4. When registering the HomeControllercontroller that came with the default L4 installation, trying to go to the page www.domain.com/homegives me a ResourceNotFoundexception. I did a composer dumpautoloadbut that did not help.

我正在从 L3 迁移到 L4。注册HomeController默认 L4 安装附带的控制器时,尝试转到该页面时www.domain.com/home出现ResourceNotFound异常。我做了一个,composer dumpautoload但没有帮助。

Did I miss out an additional step?

我错过了一个额外的步骤吗?

routes.php

路由文件

Route::controller('home', 'HomeController');

controllers/HomeController.php

控制器/HomeController.php

<?php

class HomeController extends BaseController {

    public function showWelcome()
    {
        return View::make('hello');
    }

}

Error Stacktrace

错误堆栈跟踪

NotFoundHttpException:
in /var/www/l4/vendor/laravel/framework/src/Illuminate/Routing/Router.php line 1338
at Router->handleRoutingException(object(ResourceNotFoundException)) in /var/www/l4/vendor/laravel/framework/src/Illuminate/Routing/Router.php line 992
at Router->findRoute(object(Request)) in /var/www/l4/vendor/laravel/framework/src/Illuminate/Routing/Router.php line 956
at Router->dispatch(object(Request)) in /var/www/l4/vendor/laravel/framework/src/Illuminate/Foundation/Application.php line 463
at Application->dispatch(object(Request)) in /var/www/l4/vendor/laravel/framework/src/Illuminate/Foundation/Application.php line 448
at Application->run() in /var/www/l4/public/index.php line 51

回答by juco

As per the documentation:

根据文档

Next, just add methods to your controller, prefixed with the HTTP verb they respond to

接下来,只需将方法添加到您的控制器,并以它们响应的 HTTP 动词为前缀

So:

所以:

class UserController extends BaseController {

    public function getIndex() 
    {
        // Would response to /user and /user/index
    }
}

So, in your case simply renaming showWelcome()to getWelcome()should suffice.

所以,在你的情况下,简单地重命名showWelcome()getWelcome()足够了。

回答by Micael Gustafsson

Try changing your route to this:

尝试将您的路线更改为:

Route::resource('home', 'HomeController');

UPDATE: My bad, I thought you wanted a resourceful controller, as described here: http://four.laravel.com/docs/controllers#resource-controllers

更新:我的错,我以为你想要一个足智多谋的控制器,如下所述:http: //four.laravel.com/docs/controllers#resource-controllers

For "normal" RESTful controllers juco's answer seems right.

对于“正常”的 RESTful 控制器,juco 的答案似乎是正确的。

If you want basic controllers, you can use this to correspond to your controller method:

如果你想要基本的控制器,你可以使用它来对应你的控制器方法:

Route::get('home', 'HomeController@showWelcome');