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

时间:2013-10-06 19:24:51

标签: php controller laravel laravel-4

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

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

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

在我的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);
    }
}

我收到以下错误:

  

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

当我在终端运行php artisan路线时,我得到:

+--------+--------------------------------------------------+------+----------------------------+----------------+---------------+
| 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 |                |               |
+--------+--------------------------------------------------+------+----------------------------+----------------+---------------+

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

2 个答案:

答案 0 :(得分:5)

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

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

将您的public function getRegister()更改为public function index()public function postRegister()更改为public function store()

然后可以使用index()访问GET http://localhost/laravel/register,使用store()访问POST http://localhost/laravel/register

与你同行http://localhost/laravel/

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

答案 1 :(得分:1)

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

如果你改变它也会有效

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