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
Related
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
I'm sure that I have managed to do this before but for some reason it's not working correctly.
I have a page in my project that lists clients by their account status. It's not mandatory, but a status CAN be passed via the URL, else it simply lists the active clients.
example:
myurl.com/clients/closed
would list all the clients with closed accounts.
My route is set up as follows:
app_show_clients:
path: /clients/{status}
defaults: { _controller: AppBundle:Client:showClients, status: 'active' }
requirements:
status: active|closed
And in my Twig file, I am attempting to retrieve the status as follows:
{{ app.request.get('status') }}
in order to show the status in the title of the page.
However, this just comes back as blank - though if I write:
{{ dump(app.request.get('status')) }}
It gives me the value "closed" in the var dump.
What am I missing here?
Thanks in advance
Michael
Wouldn't it be better to use the controller to handle the request properly?
function showClientsAction($status)
{
// Do some important stuff to fetch clients matching the request
// Return array assumes #Template annotation usage
return array('clients' => $clients, 'status' => $status);
}
So you could simply write {{ status }} in your template.
Did you try by this way:
{{ app.request.query.get("status") }}
Issue has been solved - I had placed the code in the wrong place, therefore I thought it had failed when it actually was working using:
{{ app.request.get('status') }}
which is the correct way.
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();
I'm just getting started with Laravel. I'm in a controller method and I say:
return \View::make('scrape', $data);
Then in scrape.blade.php I have:
#extends('layouts.master');
Finally, in layouts/master.blade.php I have:
{{ HTML::style('css/bootstrap.min.css') }}
And that where things seem to fall apart and I get:
FatalErrorException in 002eb18bb71fd3ec1de058967b799d49 line 6:
Class 'HTML' not found
What am I doing wrong? Thanks for your help.
Searching on google I found this
"By default in Laravel 5.0, Html and Form are not embedded anymore."
You need to add this package to you application.
Please use above links and last change HTML to Html.
eg:
{{ HTML::style('css/bootstrap.min.css') }}
to
{{ Html::style('css/bootstrap.min.css') }}.
its working.
{{ 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