@davestewart Whatever problem you’re trying to solve, it shouldn’t be done by manually calling a controller :)
Calling a controller from within Laravel
Is there a way to make request, using the full routing setup of your app, via Laravel, or have you got to use something like cURL or Guzzle?
I know you can do app()->call() to call a controller method, but does that work with DI, FormRequests and Route Model Binding? And I presume it almost certainly leaves out middleware?
And before anyone suggests a Service, nope, this isn't the problem I'm trying to solve :)
Thanks.
It's calling an internal API, so would need to be authed and validated at the very least!
@davestewart An API is accessed via URLs?
An API is accessed via URLs?
Yes, it (probably) will be.
To allay your fears, the wider problem I'm looking to re-work is calling controllers in Sketchpad, which are the primary units for organising code.
Right now, a request comes in, I determine the controller from the last controller scan, then use some custom code to collate objects and values before calling.
It works just fine, but I think skips middleware, and I haven't really tested for form requests or models. Additionally, I've had a recent request to see if it could be used for APIs (yes I know Postman is a better candidate).
So I just wanted to see what / if there were the options.
I'm pretty sure you are looking into your problem from a wrong angle. Take a five minute walk without thinking about it and you will probably find the solution.
As to your described case - a controller should NEVER call another controller in any case. If it is an API you are building - it has to be either service or the API has to call another API route. Anything else is just re-inventing a square wheel in my opinion.
Okay, you updated the topic while writing :D
Your used method bypasses all of the middlewares as it goes directly to the controller and not thru the route engine which would normally apply the middlewares. Also, you won't be able to have model binding too as it is done via router too.
As to your described case - a controller should NEVER call another controller in any case
Well this is what Guzzle does, just not on the same system.
it has to be either service or the API has to call another API route
Exactly my point; perhaps I should re-word the title.
As I said, I want the best way to call an endpoint, but get all the functionality of form request validation, middleware, etc
Your used method bypasses all of the middlewares as it goes directly to the controller and not thru the route engine which would normally apply the middlewares. Also, you won't be able to have model binding too as it is done via router too.
Hey @ModestasV - yes, the use case is atypical! Hence, asking the question :)
The reason why the problem has to be solved in this way, is that as a package, Sketchpad won't know how someone's application is built, so if they decide to add their app/Http/Controllers folder, I need a way to call them.
As no one has said "Oh, you can use this method" I'm thinking Guzzle would probably be the safest way to do this, that way I might even be able to send things like API tokens through (though haven't investigated).
@davestewart Heh, it's all right - we are here to help!
The problem is that I personally haven't touched the Sketchpad as I'm going against the products who injects something and makes you adapt their structure. (of course, if you were to google my name, you'd find I have such package contributions but we moved out from that logic).
So, all in all - your best option is to have an API endpoints/routes to reach the Sketchpad controllers and of course - you will be able to manipulate the data as long as you have a way to edit the return responses. The main concern is that it might be unsafe but to solve the problem - i'd make it so that the requests would only be possible from certain IP address to prevent outside connections :)
Please or to participate in this conversation.