php Lumen:在 Blade 视图中获取 URL 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31324801/
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-08-25 22:22:37  来源:igfitidea点击:

Lumen: get URL parameter in a Blade view

phplaravelbladelumen

提问by Andrea

I'm trying to get a url parameter from a view file.

我正在尝试从视图文件中获取 url 参数。

I have this url:

我有这个网址:

http://locahost:8000/example?a=10

and a viewfile named example.blade.php.

视图文件命名example.blade.php

From the controller I can get the parameter awith $request->input('a').

从控制器我可以得到的参数a$request->input('a')

Is there a way to get such parameter from the view (without having to pass it from the controller to the view)?

有没有办法从视图中获取这样的参数(而不必将它从控制器传递到视图)?

回答by Andrea

This works well:

这很有效:

{{ app('request')->input('a') }}

Where ais the url parameter.

aurl 参数在哪里。

See more here: http://blog.netgloo.com/2015/07/17/lumen-getting-current-url-parameter-within-a-blade-view/

在此处查看更多信息:http: //blog.netgloo.com/2015/07/17/lumen-getting-current-url-parameter-within-a-blade-view/

回答by Hai Nguyen

The shortest way i have used

我用过的最短方法

{{ Request::get('a') }}

回答by Ecko Santoso

More simple in Laravel 5.7 and 5.8

在 Laravel 5.7 和 5.8 中更简单

{{ Request()->parameter }}

回答by AlmostPitt

Given your URL:

鉴于您的网址:

http://locahost:8000/example?a=10

The best way that I have found to get the value for 'a' and display it on the page is to use the following:

我发现获取 'a' 值并将其显示在页面上的最佳方法是使用以下内容:

{{ request()->get('a') }}

However, if you want to use it within an if statement, you could use:

但是,如果您想在 if 语句中使用它,您可以使用:

@if( request()->get('a') )
    <script>console.log('hello')</script>
@endif

Hope that helps someone! :)

希望对某人有所帮助!:)

回答by Fred Sousa

This works fine for me:

这对我来说很好用:

{{ app('request')->input('a') }}

Ex: to get pagination param on blade view:

例如:在刀片视图上获取分页参数:

{{ app('request')->input('page') }}

回答by nrkz

Laravel 5.8

Laravel 5.8

{{ request()->a }}

回答by Nik Sumeiko

You can publicly expose Inputfacade via an alias in config/app.php:

您可以Input通过别名公开外观config/app.php

'aliases' => [
    ...

    'Input' => Illuminate\Support\Facades\Input::class,
]

And access url $_GETparameter values using the facade directly inside Blade view/template:

$_GET直接在 Blade 视图/模板中使用外观访问 url参数值:

{{ Input::get('a') }}

回答by Maksim Ivanov

As per official 5.8docs:

根据官方5.8文档:

The request() function returns the current request instance or obtains an input item:

request() 函数返回当前请求实例或获取输入项:

$request = request();

$value = request('key', $default);

Docs

文档

回答by Alexander Kim

Laravel 5.6:

Laravel 5.6:

{{ Request::query('parameter') }}