0
votes

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

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

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

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

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

When I run php artisan routes in terminal I get:

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

2

2 Answers

5
votes

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

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

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

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

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

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

1
votes

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

This will also work if you change it

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