问题
I'm trying to get a url parameter from a view file.
I have this url:
http://locahost:8000/example?a=10
and a view file named example.blade.php
.
From the controller I can get the parameter a
with $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)?
回答1:
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/
回答2:
The shortest way i have used
{{ Request::get('a') }}
回答3:
More simple in Laravel 5.7 and 5.8
{{ Request()->parameter }}
回答4:
This works fine for me:
{{ app('request')->input('a') }}
Ex: to get pagination param on blade view:
{{ app('request')->input('page') }}
回答5:
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') }}
回答6:
Laravel 5.8
{{ request()->a }}
回答7:
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
回答8:
Laravel 5.6:
{{ Request::query('parameter') }}
回答9:
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
Hope that helps someone! :)
来源:https://stackoverflow.com/questions/31324801/lumen-get-url-parameter-in-a-blade-view