How to route GET and POST for same pattern in Laravel?

PhpLaravelLaravel Routing

Php Problem Overview


Does anyone know of any way in Laravel 4 which combines these 2 lines into one?

Route::get('login', 'AuthController@getLogin');
Route::post('login', 'AuthController@postLogin');

So instead of having to write both you only have to write one since their both using the 'same' method but also the URL remains as site.com/login instead of a redirect to site.com/auth/login?

I'm curious since I remember CI has something like that where the URL remains the same and the controller is never shown:

$route['(method1|method2)'] = 'controller/$1';

Php Solutions


Solution 1 - Php

The docs say...

Route::match(array('GET', 'POST'), '/', function()
{
    return 'Hello World';
});

source: http://laravel.com/docs/routing

Solution 2 - Php

See the below code.

Route::match(array('GET','POST'),'login', 'AuthController@login');

Solution 3 - Php

You can combine all HTTP verbs for a route using:

Route::any('login', 'AuthController@login');

This will match both GET and POST HTTP verbs. And it will also match for PUT, PATCH & DELETE.

Solution 4 - Php

Route::any('login', 'AuthController@login');

and in controller:

if (Request::isMethod('post'))
{
// ... this is POST method
}
if (Request::isMethod('get'))
{
// ... this is GET method
}
...

Solution 5 - Php

You could try the following:

Route::controller('login','AuthController');

Then in your AuthController class implement these methods:

public function getIndex();
public function postIndex();

It should work ;)

Solution 6 - Php

As per the latest docs, it should be

Route::match(['get', 'post'], '/', function () {
    //
});

https://laravel.com/docs/routing

Solution 7 - Php

Route::match(array('GET', 'POST', 'PUT'), "/", array(
    'uses' => 'Controller@index',
    'as' => 'index'
));

Solution 8 - Php

In laravel 5.1 this can be achieved by Implicit Controllers. see what I found from the laravel documentation

Route::controller('users', 'UserController');

Next, just add methods to your controller. The method names should begin with the HTTP verb they respond to followed by the title case version of the URI:

<?php

namespace App\Http\Controllers;

class UserController extends Controller
{
    /**
     * Responds to requests to GET /users
     */
    public function getIndex()
    {
        //
    }

    /**
     * Responds to requests to GET /users/show/1
     */
    public function getShow($id)
    {
        //
    }

    /**
     * Responds to requests to GET /users/admin-profile
     */
    public function getAdminProfile()
    {
        //
    }

    /**
     * Responds to requests to POST /users/profile
     */
    public function postProfile()
    {
        //
    }
}

Solution 9 - Php

In Routes

Route::match(array('GET','POST'),'/login', 'AuthController@getLogin');

In Controller

public function login(Request $request){
    $input = $request->all();
    if($input){
     //Do with your post parameters
    }
    return view('login');
}

Solution 10 - Php

Right, I'm answering using my mobile, and so I haven't tested this (if I remember correctly, it isn't in the documentation either). Here goes:

Route::match('(GET|POST)', 'login',
    'AuthController@login'
);

That should do the trick. If it doesn't, then Taylor had it removed from the core; which would then mean that nobody was using it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionenchanceView Question on Stackoverflow
Solution 1 - PhpOrtegaGuillermoView Answer on Stackoverflow
Solution 2 - PhpwebnologyView Answer on Stackoverflow
Solution 3 - PhpRubens MariuzzoView Answer on Stackoverflow
Solution 4 - PhpSidView Answer on Stackoverflow
Solution 5 - PhpMehrdad HedayatiView Answer on Stackoverflow
Solution 6 - PhpRinto GeorgeView Answer on Stackoverflow
Solution 7 - PhpIgor ParraView Answer on Stackoverflow
Solution 8 - PhpAmirView Answer on Stackoverflow
Solution 9 - PhpSoubhagya Kumar BarikView Answer on Stackoverflow
Solution 10 - PhpMike RockéttView Answer on Stackoverflow