How to Call Post Methods from Laravel Controllers

Bryan Fajardo
The Startup
Published in
3 min readNov 11, 2019
Photo from Pixabay

Sometimes you may come across a need to call a post method from another function in Laravel. These methods may or may not be in the same controllers/classes.

Say you have a very complex method that accepts Illuminate\Http\Request $request as the argument. The method is normally called from a form or AJAX (Asynchronous Javascript and XML) function. So how would you go about calling that method from another method — maybe even in another class?

Below we have a very complex method we inherited and don’t want to have to rewrite. The other method is where we want to use the complex method’s return.

The Very Complex Method

The Other Method

So what can be done?

A Non-Preferred Solution

We can create a “Calling” function. Copy the actionable parts of the method into another function that can be called. In essence, you are creating a function that retrieves the data from the POST request and then calls the actual function that processes the data bypassing the data as a parameter. So now when veryComplexMethod is called, it will extract the data from the request and pass it into veryComplexMethodProcessing which is the real method.

Pros

  • Don’t Repeat Yourself — you are adhering to the DRY principle. You don’t have to rewrite any code.

Cons

  • Refactoring — You have to move things around and change things.
  • Inconsistency — Not all of your methods may need this, so you will end up with some functions using a calling function and some not.
  • In the scenario where you are consistent and all your POST methods do have a calling function, you just increased the number of functions you have to deal with. Your best bet would probably be to make a generic calling function.

A Preferred Solution

Let’s create a class that will create a request, and then execute any method we want while passing that request as an argument. In other words, we will try to mimic what a form or AJAX call would do.

For the sake of this example we will call the class POST_Caller.

Let’s start by declaring some variables.

Now, let’s setup the constructor.

Finally, let’s create the caller function.

This solution addresses some issues that the previous solution does not. First, it becomes a generic caller function that works for any method that expects a request in any class. Second, it is actually sending a Request as the argument, which may or may not be important to the functionality of the method you are calling. You also don’t have to refactor the method you are trying to call. In the previous solution we had to move the part that extracted the data from the Request to the caller function, and then pass the extracted data to the method that uses that data.

Let’s dissect the POST_Caller call() method.

app($this->class)

Here you are creating a Laravel service container instance and binding to it the class of your choice. You get some good things by using the service container this way, here are a few of them.

  1. Don’t need to use the new keyword.

2. This returns an instance of the class which you can use right away in the same line.

3. You do not need to import the class namespace into the scope of POST_Caller through the use keyword.

Plus other potential benefits that come with the service container.

{$this->method}()

Call a specific method inside of the class we passed in. Using the {} syntax we can dynamically call a method. So if my class was TestingClass::class and the method was runTest. This would be the equivalent of app(TestingClass::class)->{'runTest'}().

$this->requestClass::createFromBase($this->requestSending)

We create an Illuminate request from a Symfony Instance. We will pass in our requestClass which is most likely to be anIlluminate\Http\Request but can also be a custom FormRequest.

return $response

Then we return whatever response that method returned to us. It is up to the method that is using this class to define how to deal with the return data.

Finally, we want to put this to use. This is our POST_Caller class in action.

These are just a couple of ways we can call a method that expects a Request from another method in Laravel. There are probably many more ways to do this. If you have any questions, want to point something out, or share an alternative — comment below!

--

--

Bryan Fajardo
The Startup

Full stack software engineer. Your mind is your biggest asset, grow it!