获取参数在 Laravel 视图中直接作为变量传递到 URL 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28008011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:43:31  来源:igfitidea点击:

Get parameter passed in URL in Laravel view as variable directly?

phpurllaravel

提问by ReactingToAngularVues

Right now, if I have a parameter in a URL in one of my Laravel projects, I have to detect the route and grab the parameter:

现在,如果我在我的 Laravel 项目之一的 URL 中有一个参数,我必须检测路由并获取参数:

Route::get('mission/{name}', 'MissionsController@show');

Pass the $nameparameter as an argument to my controller:

$name参数作为参数传递给我的控制器:

class MissionsController extends BaseController {
  public function show($missionName) {
     ... 
  }

}

}

Then pass it along to the view that is returned in my controller method:

然后将其传递给在我的控制器方法中返回的视图:

return View::make('missions.mission', array(
    'name' => $missionName
));

Before then using the $missionNamevariable in my view:

然后$missionName在我看来使用该变量之前:

<p>{{ $missionName }}</p>

This is quite a roundabout way of doing so. Is there any way I can get the parameter from the URL directly in my view? I've tried accessing the $_GETsuperglobal, but it is empty. Surely there must be a better way of doing this.

这是一种相当迂回的方式。有什么办法可以直接在我的视图中从 URL 获取参数?我试过访问$_GETsuperglobal,但它是空的。当然,必须有更好的方法来做到这一点。

Thoughts?

想法?

回答by Mohebifar

Use this code :

使用此代码:

{{ Route::current()->getParameter('theParameterName') }}

EDIT: Above doesn't seem to be supported anymore in recent Laravel versions. You should use @lukasgeiter answer instead:

编辑:在最近的 Laravel 版本中似乎不再支持上述内容。您应该改用@lukasgeiter 答案:

Route::input('name');

回答by lukasgeiter

There is a nice shortcut for Route::current()->getParameter():

有一个很好的快捷方式 Route::current()->getParameter()

Route::input('name');

回答by patricus

For small projects or simple examples, it may seem like this is a "roundabout" way, however this is the way it should be. In order to create more reusable, quality code, you need to have this separation of concerns. An over-simplified idea follows.

对于小型项目或简单示例,这似乎是一种“迂回”的方式,但这是应该的方式。为了创建更多可重用、高质量的代码,您需要进行这种关注点分离。一个过于简化的想法如下。

It is your route's job to figure out which controller needs to be called, and to make sure it is called with the correct parameters.

您的路由的工作是确定需要调用哪个控制器,并确保使用正确的参数调用它。

It is your controller's job to read the state of the application (input), communicate with the model (if needed), send the data into the view, and return the response. There's plenty opinion on whether or not this violates the Single Responsibility Principle, but no need to go into that here.

控制器的工作是读取应用程序的状态(输入)、与模型通信(如果需要)、将数据发送到视图中并返回响应。关于这是否违反单一职责原则有很多意见,但无需在此赘述。

It is the view's job to use the data passed to it to build the response. The view shouldn't care where the data came from or how it was gotten, only that it now has it and can do what it needs. Your $missionNameshould be able to come from a URL segment, a URL request variable, a field on a model, or any other place you can think of, but the view shouldn't know any of that.

视图的工作是使用传递给它的数据来构建响应。视图不应该关心数据来自哪里或如何获得,只关心它现在拥有它并且可以做它需要的事情。您$missionName应该能够来自 URL 段、URL 请求变量、模型上的字段或您能想到的任何其他地方,但视图不应该知道任何这些。