handle multiple post requests to same url Laravel 5

15,790

Solution 1

Route::post('home' ,'FacebookControllers\PostsController@save');
Route::post('home' , 'FacebookControllers\MessageController@storeMessage');

won't work - how should laravel determine where to post to? The way I would go is to create two jobs and one route, then check for the value in Request and dispatch the correct job.

First, create two job classes with

php artisan make:job FacebookSave
php artisan make:job FacebookStoreMessage

The generated file will look much like this:

<?php

namespace App\Jobs;

use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;

class FacebookSaveMessage extends Job implements SelfHandling
{
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
    }
}

In the job's handle() method you can do what you wanted to do in your Controller.

Now the route, let's do

Route::post('home' ,'FacebookControllers\PostsController@findAction');

and according to this in your PostsController, add a method (I called it findAction) like this:

public function findAction(\Illuminate\Http\Request $request) {
    if ($request->has('submitPost')) {
        return $this->dispatch(new \App\Jobs\FacebookSave($request));
    } else if ($request->has('storeMessage')) {
        return $this->dispatch(new \App\Jobs\FacebookStoreMessage($request));
    }
    return 'no action found';
}

This way the correct action will be performed depending on the submitted value.

Change the job's constructor to something like:

public function __construct($data)
    {
        $this->data = $data;
    }

And you can access the submitted values from the forms you have submitted inside the job's handle() method with $this->data

Solution 2

If the purpose is only to maintain the URL that appears in the browser, you may apply a trick in which you submit your post request to different ROUTES that are specifically exist for processing purpose and then redirect back to the same URL.

For example:

Route::post('home' ,'FacebookControllers\PostsController@save');
Route::post('message' , 'FacebookControllers\MessageController@storeMessage');

Then in your view , you maybe have two diffrent forms in the same view:

<!-- the first form -->
<form action="{{url('home')}}" method="post">
Enter your post:
<input type="text" name="post"  />
<button type="submit" >Save</button>
</form>


<!-- the second form -->
<form action="{{url('message')}}" method="post">
Enter your post:
<input type="text" name="post"  />
<button type="submit" >Save</button>
</form>

Now in your controller, in the both actions do something like :

public function save(Request $request)
{
   // do stuff
    $obj->save()
    return redirect('home')->with('status' , 'you have saved your post')
}



public function storeMessage(Request $request)
{
   // do stuff
    $obj->save()
    return redirect('home')->with('status' , 'your message has been saved')
}

By doing so, the URL will remain same for the user, after the process is done the user will be redirected back to the same URL with the status.

May be this is not a good coding practice, but it solve the problem by maintaining the same URL while having multiple posts to the same controller in the same view.

The most important point here is that you have to strictly prevent an error during the process by using conditions and redirect to the same URL in case of any problem, otherwise the action will try to show the user the tricky ROUTE (yoursite.com/message) for any error.

Share:
15,790
Chaudhry Waqas
Author by

Chaudhry Waqas

Updated on June 05, 2022

Comments

  • Chaudhry Waqas
    Chaudhry Waqas over 1 year

    I have two forms on a page both submits the data via POST method. I want to handle both requests on the same url i.e /home and use different controllers and methods for both forms. Below are the two routes.

    Route::post('home' ,'FacebookControllers\PostsController@save');
    Route::post('home' , 'FacebookControllers\MessageController@storeMessage');
    

    In PostsController@save I am checking

    if(isset($_POST['submitPost']) && $_SERVER['REQUEST_METHOD']=='POST'){
    //do something
    }
    

    and in MessageController@storeMessage I am doing the same for other form

    if(isset($_POST['sendMessage']) && $_SERVER['REQUEST_METHOD']=='POST'){
                return "got it";
            }
    

    The problem is that only second route works. I don't if I am doing right or wrong. Please lead me to the right direction.

  • Chaudhry Waqas
    Chaudhry Waqas over 8 years
    thank you for introducing me to jobs, I will try it and let you know.
  • Chaudhry Waqas
    Chaudhry Waqas over 8 years
    every time I submit any form, it says 'no action found'
  • baao
    baao over 8 years
    Are you sure that you are submitting the fields you check for? @Adamnick
  • baao
    baao over 8 years
    Edited the controller, try it like this please. @Adamnick If it doesn't work, please try to dd($request) at the top of the method
  • Chaudhry Waqas
    Chaudhry Waqas over 8 years
    can you please give me a link where I can learn more about jobs?
  • baao
    baao over 8 years
    great! You're welcome! You can find more about jobs in laravel's docs here. It's in the event section, but the important parts are in there! @Adamnick