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.

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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!
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
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...
1

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

This will also work if you change it

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.