简体   繁体   English

url 参数 - laravel

[英]url parameter - laravel

our users access our site with a unique parameter on the url.我们的用户使用 url 上的唯一参数访问我们的网站。 ie http://example.com/hire-agreement?u=unique_paramhttp://example.com/hire-agreement?u=unique_param

I've set up a route to a view -我已经设置了一个视图的路线 -

Route::get('/hire-agreement', function () {
    return view('hire-agreement');
});

I have 2 questions.我有2个问题。

  1. Do I need to add anything else to the Route to allow the parameter to be read in the view?我是否需要向 Route 添加任何其他内容以允许在视图中读取参数?
  2. How do I read this parameter value in the View?如何在视图中读取此参数值? Can I use $_GET["name"]) ?我可以使用$_GET["name"])吗?

thanks Craig.谢谢克雷格。

you don't need anything more in the url section.您在 url 部分不需要更多内容。 and to use or get url parameter use laravel request() helper.并使用或获取 url 参数使用 laravel request()助手。

$value = request('key');

in view you can print a key like在视图中,您可以打印一个键,例如

{{ request('name') }}

complete example for you using request helper使用请求助手为您提供的完整示例

Route::get('/hire-agreement', function () {
    $name = request('name'); //put the key in a variable
    return view('hire-agreement', compact('name')); //send the variable to the view
});

and then in view you can use the variable as然后在视图中您可以将变量用作

{{ $name }}

if you don't want to use a variable you can use directly request helper in view如果您不想使用变量,您可以在视图中直接使用请求助手

{{ request('name') }}

you can use Request class too.你也可以使用 Request 类。

Route::get('/hire-agreement', function (Request $request) {
    $name = $request->name;
    return view('hire-agreement', compact('name'));
});

however i would suggest you to use a controller.但是我建议你使用控制器。 don't use closure in route file.不要在路由文件中使用闭包。 u can't cache them when needed.你不能在需要时缓存它们。

http://example.com/hire-agreement?u=unique_param

in laravel you can access both post and get can be access by Request class instance or request() helper so you can do在 Laravel 中,您可以访问postget可以通过Request类实例或request()助手访问,因此您可以执行

with helper request()带助手请求()

Route::get('/hire-agreement', function () {
    dd(request('u')) //  this getting from url ?u=unique_param this u param
    return view('hire-agreement');
});

with Class Request or与类请求

Route::get('/hire-agreement', function (Request $request) {
    dd($request->u)) //  this getting from url ?u=unique_param this u param
    return view('hire-agreement');
});

here you can在这里你可以

You better pass the request to a controller and handle it there, it's easier and cleaner that way.however if you want to got straight from route to view, you better use the below method.你最好将请求传递给控制器​​并在那里处理它,这样更容易和更干净。但是如果你想直接从路由到视图,你最好使用下面的方法。 put this in your route file把它放在你的路由文件中

Route::get('/hire-agreement/{param}', function ($param) {
    return view('hire-agreement')->with($param);
});

in the view you can access the param like this在视图中,您可以像这样访问参数

<p>{{$param}}</p>

now if user request "/hire-agreement/1234" your $param in the view will contain 1234, Also if you would like to access get parameters in the url you can do it like this现在,如果用户请求“/hire-agreement/1234”,您在视图中的 $param 将包含 1234,此外,如果您想访问 url 中的获取参数,您可以这样做

{{Request::input('q')}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM