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

我试图从视图文件中获取一个 url 参数。

我有个网址:

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

和一个名为 example.blade.php风景文件。

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

有没有一种方法可以从视图中获得这样的参数(而不必将它从控制器传递到视图中) ?

211353 次浏览

This works well:

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

Where a is the url parameter.

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

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

'aliases' => [
...


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

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

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

This works fine for me:

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

Ex: to get pagination param on blade view:

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

The shortest way i have used

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

Laravel 5.6:

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

More simple in Laravel 5.7 and 5.8

\{\{ Request()->parameter }}

Laravel 5.8

\{\{ request()->a }}

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:

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

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

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

As per official 5.8 docs:

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

$request = request();


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

Docs

if you use route and pass paramater use this code in your blade file

\{\{dd(request()->route()->parameters)}}

As per official documentation 8.x

We use the helper request

The request function returns the current request instance or obtains an input field's value from the current request:

$request = request();


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

the value of request is an array you can simply retrieve your input using the input key as follow

$id = request()->id; //for http://locahost:8000/example?id=10

All the answers above are correct, but there's a quickier way to do this.

\{\{request("a")}}