Vahid Amiri
Vahid Amiri

Reputation: 11117

GET and POST controller for one route in Laravel

I want to have one route rule for both GET and POST methods that redirect to one controller. Problem is GET doesn't require any parameters (it returns a view) but POST will have some parameters that are sent through a form.

In ASP.NET MVC5 I do it with one route rule and two controller methods with the same name but one of them (the POST method) has a [HttpPost] attribute and the parameters it needs while the GET method doesn't have any parameter or attribute.

How does one implement something like that in Laravel 5.x?

This is the possible controller:

public function convertUrl($somedataforpost)
{
    if(Request::isMethod('get'))
    {
        // return a view
    }
    if(Request::isMethod('post'))
    {
        // do something with POST data
    }
}

Upvotes: 0

Views: 5300

Answers (1)

user2094178
user2094178

Reputation: 9454

This is an example of how you would implement the rule:

Route::match(['get', 'post'], 'order/{invoice}/confirm', ['uses' => 'OrderController@paymentConfirmation', 'as' => 'order.payment.confirmation']);

Upvotes: 7

Related Questions