51

有谁知道 Laravel 4 中将这两条线合二为一的任何方式?

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

因此,不必同时编写两者,您只需编写一个,因为它们都使用“相同”方法,而且 URL 保持不变site.com/login而不是重定向到site.com/auth/login?

我很好奇,因为我记得 CI 有类似的东西,其中 URL 保持不变并且控制器从未显示:

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

10 回答 10

77

医生说...

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

来源: http: //laravel.com/docs/routing

于 2014-04-23T05:28:13.647 回答
44

请参阅下面的代码。

Route::match(array('GET','POST'),'login', 'AuthController@login');
于 2015-02-18T12:07:27.843 回答
35

您可以使用以下方法组合路由的所有 HTTP 动词:

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

这将匹配GETPOSTHTTP 动词。它也将匹配PUT, PATCH& DELETE

于 2013-08-20T02:42:55.147 回答
18
Route::any('login', 'AuthController@login');

在控制器中:

if (Request::isMethod('post'))
{
// ... this is POST method
}
if (Request::isMethod('get'))
{
// ... this is GET method
}
...
于 2014-12-22T23:59:51.067 回答
10

您可以尝试以下方法:

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

然后在你AuthController class实现这些方法:

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

它应该工作;)

于 2015-09-14T21:29:01.300 回答
8

根据最新的文档,它应该是

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

https://laravel.com/docs/routing

于 2019-04-10T08:30:38.657 回答
5
Route::match(array('GET', 'POST', 'PUT'), "/", array(
    'uses' => 'Controller@index',
    'as' => 'index'
));
于 2014-03-16T20:43:56.883 回答
1

在 laravel 5.1 中,这可以通过隐式控制器来实现。查看我从 laravel 文档中找到的内容

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

接下来,只需将方法添加到您的控制器。方法名称应以它们响应的 HTTP 动词开头,后跟 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()
    {
        //
    }
}
于 2015-11-08T19:41:09.677 回答
1

在路线中

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

在控制器中

public function login(Request $request){
    $input = $request->all();
    if($input){
     //Do with your post parameters
    }
    return view('login');
}
于 2020-05-15T09:27:54.550 回答
-1

是的,我正在用我的手机回答,所以我没有测试过这个(如果我没记错的话,它也不在文档中)。开始:

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

这应该够了吧。如果不是,那么泰勒将其从核心中移除;这意味着没有人在使用它。

于 2013-08-20T05:42:50.017 回答