I understand that I use a Request object from "Illuminate\Http\Request;" But there are some things I just don't get about it.
How do I create a Request object to be passed into any given function that needs it? For the sake of using anything I may need? (e.g $request->session()->get('key', 'default')).
Why/how is this object necessary for interfacing with other files in my laravel project? Apparently sessions are the key to using project-wide variables, while request objects are how we access those? I feel I'm barely scratching the surface of it with this understanding.
Any help and/or clarity would be greatly appreciated
@MUNAZZIL - Where does the "Request $request" come from, though? I can create a function argument like that, fine, but then I have to call the function and give it an object. How does that part work out?
@UCCDEV - It comes from the Service Container a pattern called dependency injection. The whole process cannot be explained in a comment here. You can read the documentation and there is a great resource that covers all of this, the book by Matt Staufer Laravel up and running.
In your method the $request will be automatically filled with the data that you have passed, so $request->param will equal 1. This is done by the service container, it is a magic that happens behind the scenes. As I said this topic is huge and not easy to explain, you should read the resources that I have shared they give good examples in there. Unless you like to read the source code :)
@NAKOV - I think I see. Though in this instance, how could I use a Request object for test cases? Those don't use URL routing, if I recall correctly, they're called from the command line
because request()->all() returns data like $_POST and $_GET whereas dd($request); is an object. That data is stored in the object as a class property @uccdev
Just think that $request as a wrapper for all the http request parameters, where it include a lot of useful functions to easier your work. if you need more details or example, you could read the documentation or inspect the code.
Enlightening, thank you. Though in my instance, I want to use a secret access token as a project-wide variable. Since it is a secret, and something the back-end should know, I don't want it passed in through the URL; it's not something that $_GET or $_POST should see. Maybe request objects aren't really the answer, if I want something like that. Any recommendations?