Laravel link_to attributes aren't working for me..? - php

I'm using the following...
$params = array('amount' => $transaction_details['AMT']);
echo link_to('/transaction/'.$transaction_details['TRANSACTIONID'].'/refund', 'Refund', $params);
I'm getting my link that goes to the correct URL, but it's not adding URL parameters to it at all, which is what I thought the 3rd attribute would do where I'm passing in the $params array. I can verify that $transaction_details['AMT'] does have a value.
Any info on what I'm doing wrong here would be greatly appreciated. Thanks!

I think you misunderstood the 3rd parameter. The attributes refer to the actual HTML element. For example:
$params = array('class' => 'btn');
echo link_to('/link', 'I am a link', $params);
Would result in:
I am a link

#geevCookie is correct. To add the parameters to your URL you can use the URL helper.
echo link_to(url('/transaction/'.$transaction_details['TRANSACTIONID'].'/refund', $params), 'Refund');

Related

How to add multiple 'Get' on URL?

Im working on a php project and first a have a value on url for example :
http:www.c/com/app.php?doctor_id=12
And then i want to add an another value on that url without losing the doctor id
For example : http:www.c/com/app.php?doctor_id=12?appoin_date=11pm
Also when i get the doctor id it redirects the appoin page :)
Do not generate a URL-encoded query string yourself. Use the http_build_query function for this. The function also takes over the necessary escaping of the parameters. Example:
$url = 'http:www.example.com/app.php';
$parameters = [
'doctor_id' => 12,
'appoin_date' => '11pm',
'name' => 'max&moritz'
];
$url .= '?'.http_build_query($parameters);
//"http:www.example.com/app.php?doctor_id=12&appoin_date=11pm&name=max%26moritz"

Access id of paginator in controller

Hello guys I am new to Zendframework2.
I have the following line in view\partial\paginator.phtml
<a href="<?php echo $this->url($this->route);?>?page=<?php echo $page; ?>?id=<?php echo $this->id?>">
In browser it looks like http://new_project.localhost/districts?page=2?id=4
new_project is the name of my project, districts is route of controller and
in id=4, 4 is id which I want to access in controller.
I tried:
$_GET['id'];
$this->params()->fromRoute('id'); //also
$this->params()->fromQuery('id'); //also
But non of these works.
How I will access this id in controller?
I solve it, just by putting condition like below:
if (isset($_GET['id']))
{
// perform operation here.
}
Don't build your A element tag href yourself but use the framework like you should.
Url view helper:
// $this->url($routeName, $routeParams, $options, $reuseMatchedParams = false)
$this->url('my/route', ['id' => $object->getId()], ['query' => ['page' => $page]])
Controller plugin:
$this->params('id') // will take ID either from routeParameters or queryParameters.
$this->params()->fromRoute('id') // will take the id from the routeParameters
$this->params()->fromQuery('page') // will take the page from the route query
So when you've got a route with a routeParameter id but your query also has the id within it's query its best practice to keep on using the $this->params()->fromRoute('id') or $this->params()->fromQuery('id') so you always know where the value comes from instead of using $this->params('id').
As with the params plugin you can also define default values on the second argument, like: $this->params()->fromQuery('page', 1). So page will always be 1 if the user did not provide one. The default of the second argument is null. Hope this helps and makes you stop using $_GET['bla']
I think your url should look like this:
http://new_project.localhost/districts?page=2&id=4
^
not ?
Because of the mistake you made the parser probably doesn't parse the id in your url correctly meaning you cannot access it like you tried.
It would be better to use the url view helper for rendering the query parameters in the url then you wouldn't make such a mistake. You can check this post on StackOverflow for reference on how to add query parameters to a url using the url view helper. In your case it would look something like this:
<?php
$href = $this->url($this->route, [], ['query' => [
'page' => $page,
'id' => $id,
]]);
?>
<a href="<?php echo $href ?>">
Now you get the correct url:
http://new_project.localhost/districts?page=2&id=4
Meaning the parser also manages to parse the id correctly.
You should be able to access it now like you tried with:
$this->params()->fromQuery('id'); //returns your id from the url

CakePHP redirect form post with querystring

I'm a Cake newbie, and I'm looking to post a querystring value into a controller method, but always reload the view with the querystring intact. Currently I have the below snippet.
public function something()
{
if($this->request->query !=null )
$date = $this->request->query["date"];
}
<?php echo $this->Form->create('setup',array('action' => 'something?date=2013','id'=>'setup-form','role'=>'form') ); ?>
Any advice on why something() doesn't redirect to something?date=2013 on its default render? Do I need to do some special routing?
In CakePHP 2, you can include query string parameters in $url parameters like so:
array('action' => 'something', '?' => array('date' => '2013'))
CakePHP will build the query string and append it to the matched URL in your routing configuration.
(Note: You may need to pass FormHelper::create an entire URL, generated from HtmlHelper::url, instead of using the "shorthand" technique.)

How to append GET parameters to the link_to_route function in laravel

Is it possible to add get parameters to a named route in laravel?
I have the following code in my routes.php file
Route::controller('store', 'StoreController',
array(
'getSearch' => 'store.search'
)
);
I want to construct a link such as domain.com/store/search?keyword=rolex using the link_to_route function.
Thanks
link_to_route takes a third argument, parameters.
So you can try to do this:
link_to_route('store.search', 'Search', array('keyword' => 'rolex'))
You can get really close url with '/' after search so instead of:
domain.com/store/search?keyword=rolex
you can get:
domain.com/store/search/?keyword=rolex
What you need in this case is:
link_to_route('store.search', 'Search', array('keyword' => '?keyword=rolex'))
but it will change ? into %3F so you should probably use in this case:
urldecode(link_to_route('store.search', 'Search', array('keyword' => '?keyword=rolex')))

cakephp 2 - pagination - don't pass variable in url

I have a search form which, if someone fills it in and there are no results to report, I want to make them aware of that but then show them a result set of my choosing. The problem is that when I show them this result set of my choosing and I use the paginator helpers for prev/next they want to pass the search term which is in my URL along with the page number.
That obviously causes problems because there isn't a page:2 for the results search:termHasNoResults
How can I stop the paginator helper passing my search variable in the URL when I'm showing them my alternative result set?
Thanks
Use
$this->Paginator->options('url' => array(
'controller' => 'YOUR CONTROLLER', 'action' => 'YOUR ACTION', 'OTHER PARAMS');
You can pass 'url' option to pagination in your view like this:
$url = array_merge($this->request->pass, $this->request->named);
unset($url['page']);
$parts = explode('?', $_SERVER['REQUEST_URI'], 2);
if (count($parts) == 2) {
$url['?'] = $parts[1];
}
$this->Paginator->options(array(
'url' => $url,
));
More here: http://api.cakephp.org/2.2/class-PaginatorHelper.html
and here: http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::options
If you widely use filters and additional url params - make an element which will handle it for you

Categories