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

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

How to have twig path() with silex slug?

In silex I have something like
$controllers->get('/{id}', 'Controllers\\Login::index')->bind('login');
when in twig I try to get path('login') I get exception
("Some mandatory parameters are missing ("id") to generate a URL for route "login"."). ?
I know this is because of {id} and I have to pass a second parameter to path() but how should it look like?
In order to pass parameters to twig path, use the following syntax:
{{ path('login', {'id': 'your-id-here'}) }}
you can have a look at the documentation here:
Path() function documentation, symfony
You can pass several parameters as explained here:
SO: several paremeters in twig

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

Lumen: get URL parameter in a Blade view

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();

Laravel 5: Class 'HTML' not found

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.

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