0

我正在尝试使用 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 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 |                |               |
+--------+--------------------------------------------------+------+----------------------------+----------------+---------------+

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

4

2 回答 2

5

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

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

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

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

http://localhost/laravel/你的改变。

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

于 2013-10-06T19:31:39.663 回答
1

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

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

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

于 2013-10-24T21:46:43.100 回答