在laravel Route :: get('user/{id}'中,"id"来自哪里? [英] In laravel Route::get('user/{id}', where does "id" come from?

查看:64
本文介绍了在laravel Route :: get('user/{id}'中,"id"来自哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个学习新花样的老海狗.

I am an old sea-dog learning new tricks.

给出以下路线: Route :: get('user/{id}','UserController @ showProfile'); "id"的值在哪里?来自?这是变量$ id:

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]);
}

推荐答案

为了给出即使是老海狗也可以理解的解释,我们需要使用php.
让我们举一个简单的例子,我们有两个文件 user.php profile.php user.php 中,我们有一个指向profile.php

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>

然后,我们可以使用 $ _ GET $ _ SERVER

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;

为了在HTTP请求的面向对象表示中实现类似的概念,我们使用Request类,该类允许以更简单的方式与HTTP请求进行交互.(有关更多详细信息,请参见

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 Symphony)

首先,交响乐(至少据我所知)提出了以有序和更好的面向对象方式使用http请求的概念. Symfony \ Component \ HttpFoundation \ Request 类.您可以使用其中一种方法从php检索与$ _GET相同的输出

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 symphony, 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 Symphony Request Object for more methods.

现在进入主要部分,控制器如何访问uri(routes)中传递的这些参数.
为此,我们需要查看类Illuminate/Http/Request ,如您所见,它扩展了 Symfony \ Component \ HttpFoundation \ Request作为SymfonyRequest ,它继承了该类的所有基本请求属性,因此您可以访问 Symphony Request Object 包括诸如get()之类的方法,该方法从uri中检索参数.

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 Symphony Request Object including the methods such as get() which retrieves the parameter from the uri.

控制器

控制器扩展了BaseController,它只是一个抽象类 Illuminate \ Routing \ Controller ,它具有名为匹配的方法(请求$ request,$ includeMethod = true),它确定路由是否与给定的请求以及 hasParameter() hasParameter($ name),确定路由是否具有您的问题'user/{id}'中提到的参数,或者该路由中是否存在给定的参数.

确定后,称为参数($ name,$ default = null)有助于获取参数(这回答了您的问题127.0.0.1:8000/user/1中的"1"从何而来?)到控制器的路线和操作可以在此执行.

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.

这篇关于在laravel Route :: get('user/{id}'中,"id"来自哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆