laravel 4 Route::controller() 方法返回 NotFoundHttpException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19212938/
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 08:31:42  来源:igfitidea点击:

laravel 4 Route::controller() method returns NotFoundHttpException

phpcontrollerlaravellaravel-4

提问by Clay Allen

I am trying to route to a RESTful controller using the following in app/routes.php:

我正在尝试使用 app/routes.php 中的以下内容路由到 RESTful 控制器:

Route::controller('register', 'RegisterController');

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

In my app/controllers/RegisterController.php file I have added the following:

在我的 app/controllers/RegisterController.php 文件中,我添加了以下内容:

<?php

class RegisterController extends BaseController 
{
    public function getRegister()
    {
        return View::make('registration');
    }

    public function postRegister()
    {
    $data = Input::all();
    $rules = array(
        'first_name' => array('alpha', 'min:3'),
        'last_name' => array('alpha', 'min:3'),
        'company_name' => array('alpha_num'),
        'phone_number' => 'regex:[0-9()\-]'
    );  
    $validator = Validator::make($data, $rules);
    if ($validator->passes()) {
        return 'Data was saved.';
    }
    return Redirect::to('register')->withErrors($validator);
    }
}

I am getting the following error:

我收到以下错误:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

Symfony\Component\HttpKernel\Exception\NotFoundHttpException

When I run php artisan routes in terminal I get:

当我在终端中运行 php artisan routes 时,我得到:

+--------+--------------------------------------------------+------+----------------------------+----------------+---------------+
| Domain | URI                                              | Name | Action                     | Before Filters | After Filters |
+--------+--------------------------------------------------+------+----------------------------+----------------+---------------+
|        | GET /register/register/{v1}/{v2}/{v3}/{v4}/{v5}  |      | Register@getRegister       |                |               |
|        | POST /register/register/{v1}/{v2}/{v3}/{v4}/{v5} |      | Register@postRegister      |                |               |
|        | GET /register/{_missing}                         |      | Register@missingMethod     |                |               |
|        | GET /                                            |      | HomeController@showWelcome |                |               |
+--------+--------------------------------------------------+------+----------------------------+----------------+---------------+

I don't understand why register is showing twice in the URI and the second GET action is missing and why I am getting this error.

我不明白为什么 register 在 URI 中显示两次而第二个 GET 操作丢失以及为什么我收到此错误。

回答by devo

If you are using RESTful API, the best way is in your route,

如果您使用的是 RESTful API,最好的方法是在您的路线中,

Route::resource('register', 'RegisterController');

And change your public function getRegister()to public function index()and public function postRegister()to public function store()

并更改您的public function getRegister()topublic function index()public function postRegister()topublic function store()

Then the index()can be access using GET http://localhost/laravel/registerand the store()using POST http://localhost/laravel/register

然后index()可以访问 usingGET http://localhost/laravel/registerstore()usingPOST http://localhost/laravel/register

Chaneg the http://localhost/laravel/with yours.

http://localhost/laravel/和你一起换。

And the same way the update($id)is used for update and destroy($id)is used for delete

update($id)用于更新和destroy($id)用于删除的方式相同

回答by Hassan Jamal

Route::controller('register', 'RegisterController');

路由::控制器('注册', '注册控制器');

This will also work if you change it

如果您更改它,这也将起作用

Route::controller('/', 'RegisterController');

Route::controller('/', 'RegisterController');