DEV Community

Discussion on: Restructuring a Laravel controller using Services & Action Classes

Collapse
 
balajidharma profile image
Balaji Dharma • Edited

You can create Request object on the fly and call the action function

use Illuminate\Http\Request;

$request = new Request([
        'name'   => 'User',
        'email' => 'newuser@example.com',
    ]);

$createUser->handle($request);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
davorminchorov profile image
Davor Minchorov

Sure, you can but theoretically this does not make much sense in the real world because the CLI commands do not do HTTP requests.

There are some MVC (or any architecture) rules about what part of the application the HTTP Request object has access to, which is usually the middleware and controller classes.

Another issue with the service method having the Request object as a parameter is that you are dragging more things with you that you do not actually need to. The only things you need is the request data but you are dragging methods and properties related to headers, session, cookies, validation etc which you do not actually need.

Thread Thread
 
balajidharma profile image
Balaji Dharma • Edited

For Laravel CLI, we going to call the actions handle function in command controller (not going to use the actions or service controller directly). So, that time we can create request objects on the fly.

I am agree using plain object or array will improve the performance, but really we don’t know how much it improves. It based on application complexity.