57

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';
tereško
  • 58,060
  • 25
  • 98
  • 150
enchance
  • 29,075
  • 35
  • 87
  • 127

11 Answers11

84

The docs say...

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

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

OrtegaGuillermo
  • 889
  • 1
  • 6
  • 4
53

See the below code.

Route::match(array('GET','POST'),'login', 'AuthController@login');
Gunaseelan
  • 2,494
  • 5
  • 36
  • 43
webnology
  • 539
  • 4
  • 2
38

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.

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
  • How would you check if the verb is a `GET` or `POST`? – enchance Aug 20 '13 at 03:26
  • You can always use `$_SERVER['REQUEST_METHOD'] === 'POST'`, but I will not recommend to mix in one action two logic... the idea with controller is to **separate the logic** in conjunction with Post-Redirect-Get pattern. Take 2 or 3 mins to read this specific thread: https://github.com/laravel/laravel/pull/1517. – Rubens Mariuzzo Aug 20 '13 at 10:43
  • 2
    @enchance , you can check is it `GET` or `POST` with: `if (Request::isMethod('post'))` `{` `//...` `}` – Sid Dec 22 '14 at 23:52
21
Route::any('login', 'AuthController@login');

and in controller:

if (Request::isMethod('post'))
{
// ... this is POST method
}
if (Request::isMethod('get'))
{
// ... this is GET method
}
...
Sid
  • 4,302
  • 3
  • 26
  • 27
11

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 ;)

Mehrdad Hedayati
  • 1,434
  • 13
  • 14
  • 10
    Note from the future: [implicit controllers are deprecated in Laravel 5.2 and will be removed in the future](http://benjaminkohl.com/post/implicit-controller-routing-is-deprecated-in-laravel-5-2). – jojonas Apr 21 '16 at 12:03
8

As per the latest docs, it should be

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

https://laravel.com/docs/routing

Duke
  • 35,420
  • 13
  • 53
  • 70
  • How is this any different from the [most upvoted answer](https://stackoverflow.com/a/23235478) made 5 years prior? – gre_gor May 12 '22 at 02:46
6
Route::match(array('GET', 'POST', 'PUT'), "/", array(
    'uses' => 'Controller@index',
    'as' => 'index'
));
Igor Parra
  • 10,214
  • 10
  • 69
  • 101
2

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');
}
Soubhagya Kumar Barik
  • 1,979
  • 20
  • 26
1

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()
    {
        //
    }
}
Amir
  • 8,821
  • 7
  • 44
  • 48
1

Use match to handle both methods

Route::match(['GET','POST'], 'users', UserController@store);
Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
rakeshboliya
  • 103
  • 12
-2

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.

Mike Rockétt
  • 8,947
  • 4
  • 45
  • 81