0

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Clay Allen
  • 55
  • 2
  • 9

2 Answers2

5

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

Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
  • Works Perfectly! Thanks for the fast and insightful response. I guess I need to research the difference of using Route::resource() vs Route::controller(). Cheers! – Clay Allen Oct 06 '13 at 20:32
  • @GeekyGoon if you like, check some of my similar answers here http://stackoverflow.com/questions/19196636/laravel-restful-controller-parameters/19196884#19196884 and http://stackoverflow.com/questions/19190389/laravel-4-remove-index-on-default-getindex-controller-function/19193669#19193669 then http://stackoverflow.com/questions/19124018/laravel-retrieving-data-from-rest-api/19132837#19132837 – Anshad Vattapoyil Oct 07 '13 at 03:13
  • If you want to go `Route::controller('register', 'RegisterController')` way, you can use the function names `getIndex` and `postIndex` to achieve the same results. see: [Restful-controllers](http://laravel.com/docs/controllers#restful-controllers) – tharumax Oct 07 '13 at 04:04
  • Thanks, after both of your responses I realize it makes more sense to allow the UsersController to handle the creation of the new user. I'm getting it all together... – Clay Allen Nov 26 '13 at 07:25
1

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

This will also work if you change it

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

Hassan Jamal
  • 694
  • 9
  • 11