Lumen: get URL parameter in a Blade view - php

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)?

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/

The shortest way i have used
{{ Request::get('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

More simple in Laravel 5.7 and 5.8
{{ Request()->parameter }}

Laravel 5.8
{{ request()->a }}

This works fine for me:
{{ app('request')->input('a') }}
Ex: to get pagination param on blade view:
{{ app('request')->input('page') }}

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') }}

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

All the answers above are correct, but there's a quickier way to do this.
{{request("a")}}

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

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

if you use route and pass paramater use this code in your blade file
{{dd(request()->route()->parameters)}}

here is the code to get filtered data with pagination
$queryvariable->appends($data)->links('link of new page');
note there
$data = $request->all();

Related

Issues while trying to access url parameters with Twig [duplicate]

I have a url: http://myserver.com/mypage?vid=1234
I want to output 1234 on to the page. I have tried this but it is not working:
{{ app.request.get('vid') }}
what am I doing wrong or missing?
You can do this by using the "query" object :
{{ app.request.query.get('vid') }}
But you should get this value in the controller and then pass it to twig.
EDIT : This answer is based on the asumption that you are using Symfony Framework

How to pass url parameters to views with Route::view method in Laravel?

This is my code in Laravel :`
Route::view('/welcome/{name}','welcome',['name'=>'name']);
I want to know is it possible get and pass {name} parameter to view by Route::view or not?
I know that we can do it with Route::get :
Route::get('/welcome/{name}', function($name){
return view('welcome',['name'=>$name]);
});
Instead of trying to pass parameter use the segment. Try this in your view:
{{ Request::segment(2) }}

Equivalent $request->path() in pure php

Assume this url:
http://localhost/myproject/public/add/11
The result of this code: echo $request->path() is add/11. Now I want to know how can I get that value in pure php?
Because I need to use that value in a view which isn't called by a controller (it is a master view).
Since you're using blade, try this:
{{ request()->path() }}
Since it's a Blade view, you can use Request facade:
Request::path()
Or global helper:
request()->path();
$path_only = parse_url('http://localhost/myproject/public/add/11', PHP_URL_PATH)
echo $path_only;
and result :
/myproject/public/add/11

twig: How to get request string?

I have a url: http://myserver.com/mypage?vid=1234
I want to output 1234 on to the page. I have tried this but it is not working:
{{ app.request.get('vid') }}
what am I doing wrong or missing?
You can do this by using the "query" object :
{{ app.request.query.get('vid') }}
But you should get this value in the controller and then pass it to twig.
EDIT : This answer is based on the asumption that you are using Symfony Framework

how to pass all request query parameters to embedded controllers in twig symfony 2?

{{ render(controller("SomeBundle:Foo:Bar", {HERE I WANT TO PASS ALL query parameters app.request.query.all}) }}
So can I access all master request query parameters in sub request and the subrequest should also run independently?
Try this:
{{ render(controller("SomeBundle:Foo:bar", {'all': app.request.query.all}) }}
and in action store it in $all variable
public function barAction($all) {
// other your code
}
From your controller:
array_merge($request->query->all(), $request->get('_route_params'));
//query->all : get all query string parameters
//_route_params : get current route parameters
From your twig template must be sth like:
app.request.query.all|merge(app.request.attributes.get('_route_params'))
I've never used this in twig templates, so first test it ;)
Then you can use that functions however you want to build the variables you'll pass to your subrequest
To just pass in what is in app.request.query.all:
{{ render(controller("SomeBundle:Foo:Bar", app.request.query.all)
To merge something extra in:
{{ render(controller("SomeBundle:Foo:Bar", { something: 'extra' }|merge(app.request.query.all))
Tested in Symfony 3.3.10 and Twig 1.35.0

Categories