How to invoke a route "manually"

Hi all,

With Slim App get/post/patch etc functions, I can register routes and Slim will invoke them automatically.
My question is how can I invoke those routes from my code?
What I want to do is to batch the requests.
For example, I may have routes such like
get(’/a’);
get(’/b’);
Then I may have a get(’/batch’), the batch route parses a JSON data to extract a sequence of sub request such as /a and /b, then it invoke the routes on /a and /b manually.

Thanks

$app->subRequest is the answer.

The sub-request functionality will not be available in Slim 4.

Thanks. Then what’s the alternative?

You probally need to review your current design.

@wqking The answer is to refactor your application to use best practices.

You can use the same action from different routes.

But why have you this issue?
I think is a design issue as @geggleto and @SVL said you should refactor your application.

@SVL, @geggleto, @damianopetrungaro, thanks for your advice.
But I can’t see the issue in the design of “batch” idea.
Assume I have API f1 and f2, and 3 pages A, B, C. A uses f1, B uses f2, and C uses both f1 and f2. In page C, instead of sending two requests, I would like to batch f1 and f2 in one single request to save HTTP connections.
Thoughts?

@wqking sorry but this could be a bad design idea.

You can abstract the two actions in two methods of a service.
So the content of f1and f2, will be available from the service->f1 and service->f2.

If you explains to us the real example, we’ll help you in a better way.

I mean, yes, I do provide two methods f1 and f2, but whenever the client app needs both f1 and f2, I want to batch them in a single HTTP request. The sever still calls the handlers of f1 and f2 respectively. My purpose to do it is to save HTTP connections (1 connection for both f1 and f2 instead of 2 for f1 and f2).
If anyone in doubt googles “restful batch”, there are pretty some discussion on this topic.
Any way, since Slim is a framework, I wish it keeps the subRequest API because it’s useful even though the Slim developers don’t think so.

Batch requests can be handled in other way.
If you explains your real example we can help you in a better way :smiley:

The answer is always better design. :slight_smile: More abstraction.

1 Like