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

前端 未结 10 1913
余生分开走
余生分开走 2020-12-24 10:55

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

Route::get(\'login\', \'AuthControl         


        
相关标签:
10条回答
  • 2020-12-24 11:31
    Route::any('login', 'AuthController@login');
    

    and in controller:

    if (Request::isMethod('post'))
    {
    // ... this is POST method
    }
    if (Request::isMethod('get'))
    {
    // ... this is GET method
    }
    ...
    
    0 讨论(0)
  • 2020-12-24 11:34

    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.

    0 讨论(0)
  • 2020-12-24 11:38

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

    0 讨论(0)
  • 2020-12-24 11:42

    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');
    }
    
    0 讨论(0)
  • 2020-12-24 11:43

    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()
        {
            //
        }
    }
    
    0 讨论(0)
  • 2020-12-24 11:44

    See the below code.

    Route::match(array('GET','POST'),'login', 'AuthController@login');
    
    0 讨论(0)
提交回复
热议问题