60

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

11 Answers 11

89

The docs say...

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

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

2
  • 3
    This answer is more precise. Dec 23, 2014 at 3:40
  • 7
    I'm sorry but how is this getting so many upvotes? Yes it explains how you use both GET and POST but says nothing about how to call the different methods depending on the request type.
    – lennard
    Apr 16, 2016 at 12:58
55

See the below code.

Route::match(array('GET','POST'),'login', 'AuthController@login');
1
  • 3
    The only answer to properly answer the question.
    – myol
    Jan 28, 2016 at 10:51
39

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.

3
  • How would you check if the verb is a GET or POST?
    – enchance
    Aug 20, 2013 at 3: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: github.com/laravel/laravel/pull/1517. Aug 20, 2013 at 10:43
  • 2
    @enchance , you can check is it GET or POST with: if (Request::isMethod('post')) { //... }
    – Sid
    Dec 22, 2014 at 23:52
22
Route::any('login', 'AuthController@login');

and in controller:

if (Request::isMethod('post'))
{
// ... this is POST method
}
if (Request::isMethod('get'))
{
// ... this is GET method
}
...
1
  • 3
    This is a better answer. Thanks
    – muya.dev
    Jul 17, 2017 at 14:31
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 ;)

1
8

As per the latest docs, it should be

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

https://laravel.com/docs/routing

1
6
Route::match(array('GET', 'POST', 'PUT'), "/", array(
    'uses' => 'Controller@index',
    'as' => 'index'
));
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');
}
2

Use match to handle both methods

Route::match(['GET','POST'], 'users', UserController@store);
2
  • How is this answer any different from the existing ones?
    – gre_gor
    Oct 27, 2022 at 14:38
  • @gre_gor One thing that tripped me up was that the controller is no longer passed as a string, rather as the object itself. I tried old code and it didn't work in Laravel 10 as a string.
    – Exit
    Oct 17, 2023 at 10:04
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()
    {
        //
    }
}
-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.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.