Reputation: 169
I am an old sea-dog learning new tricks.
Given this route:
Route::get('user/{id}', 'UserController@showProfile');
where does the value of "id" come from? It is the variable $id in:
public function show($id)
{ $post=\DB::table('events')->where('id',$id)->first();
return view('post',['post'=>$post]);
}
Upvotes: 0
Views: 2462
Reputation: 3420
In order to give explanation that can be understood by even a old sea-dog, we need to go from php.
Lets take a simple example, we have two files user.php
and profile.php
In user.php
, we have a simple link to profile.php
<a href="profile.php?id=<?php echo $id;?>"style="text-decoration: none"><?php echo $nume;?></a>><?php echo $id;?></a>
Then we can access the id and uri in profile.php
using $_GET
and $_SERVER
$uri = $_SERVER['REQUEST_URI'];
$id = $_GET['id'];
echo $id . "<br>" . $uri;
To implement similar concept in object-oriented representation of the HTTP request, we use Request class which allow to interact with the HTTP request in an easier way.(For more details, you can see Documentation on Http fundamentals by Symfony)
The concept of using http request in ordered and better way of object orientation was first provided by (at least as per my knowledge) by symfony, in the
Symfony\Component\HttpFoundation\Request
class. You can retrieve the same output as $_GET from php using one of its method
$request->query->get('id'); // retrieves $_GET and $_POST variables respectively
You can see Symfony Request Object for more methods.
Now coming to the main portion how these parameters passed in the uri(routes) are accessed by controllers.
For that we need to see the class Illuminate/Http/Request,
As you can see it extends Symfony\Component\HttpFoundation\Request as SymfonyRequest
and it inherits all the basic request properties of this class, so you get access to all the properties of Symfony Request Object
including the methods such as get() which retrieves the parameter from the uri.
Controller
The controller extends BaseController which is nothing but an abstract class Illuminate\Routing\Controller
which has a method named callAction($method, $parameters) which is responsible for executing an action on the controller.
If we go a little deeper, we can see that it is called by the method called dispatch(Route $route, $controller, $method) in Illuminate\Routing\ControllerDispatcher
class, it is the main method which dispatches a request to the given controller and method.
As you can see it has an argument named $route from \Illuminate\Routing\Route
This Route class has methods such as matches(Request $request, $includingMethod = true) which determines if the route matches a given request along with methods such as hasParameter()
and hasParameter($name)
which determines if the route has parameter like the one mentioned in your question 'user/{id}'
or the given parameter exists in the route.
Once determined, the method called parameter($name, $default = null) helps to obtain the parameters(this answers your question Where does the "1" in 127.0.0.1:8000/user/1 come from? ) from the route to the controller and action can be permformed on that.
Upvotes: 3