Aristona
Aristona

Reputation: 9341

Laravel3: How can I forward post methods to action of controller?

Route::get('admin/user/add', function()
{
    if (!Input::has('submit'))
        return View::make('theme-admin.user_add');
    else
        Redirect::to('admin@user_add_process');
});

Normal GET request to admin/user/add shows the register form. However, when the form is submitted, I have to redirect it to action_user_add_process() function of Admin controller so I can save values to database.

The solution above doesn't work and I get 404's.

I call form action like this:

action="{{ URL::to('admin/user/add') }}">

How can I solve this issue?

Ps. If there is a shorter way to achieve this, let me know!

Upvotes: 0

Views: 131

Answers (1)

David Barker
David Barker

Reputation: 14620

You need to specify that you are redirecting to a controller action using the to_action() method of Redirect.

return Redirect::to_action('admin@user_add_process');

Alternatively, you could just use this URL that doesnt even use the route you created making the if/else irrelevant.

{{ URL::to_action('admin@user_add_process') }}

On a third note, keeping your routes clean makes maintainence alot easier moving forward. As routes use a restful approach, take advantage of it. Using the same URL you can create and return the View with a GET request and submit forms with a POST request.

Route::get('admin/user/add', function() { ... }

Route::post('admin/user/add', function() { ... }

You can also have your routes automatically use a controller action like this:

Route::post('admin/user/add', 'admin@user_add_process');

Upvotes: 2

Related Questions