can't maintain question mark in query string - php

I'm using drupal's l() function to build a link. So I'm passing this variable as a second argument that the function will generate the href value from.
$performer = $row->node_field_data_field_evenement_performer_title;
$url = 'node/' . $row->node_field_data_field_evenement_performer_nid . '/lightbox2';
print l($performer, $url, array('html' => TRUE, 'attributes' => array('rel' => 'lightframe[group|width:500px; height: 500px][caption]', 'class' => 'performer-link')));
The URL should end with the query string ?format=simple. So basically my $url variable should be updated something like:
$url = 'node/' . $row->node_field_data_field_evenement_performer_nid . '/lightbox2?format=simple';
No matter what encode/decode function I wrap around, and whatever escaping I do, that question mark and equals sign keep getting interpreted as %25 types of characters.
I tried:
$url = 'node/' . $row->node_field_data_field_evenement_performer_nid . '/lightbox2\?format\=simple');
or
'/lightbox2' . any_decode_or_encode_function_outthere('?format=simple');
but I keep getting URLs like node/202/lightbox2%5C%3Fformat%5C%3Dsimple.

Use the query key for the options array passed to l():
print l($performer, $url, array(
'html' => TRUE,
'attributes' => array(
'rel' => 'lightframe[group|width:500px; height: 500px][caption]',
'class' => 'performer-link'
),
'query' => array(
'format' => 'simple'
)
));
l() calls url() internally, and options can be forwarded on. See the docs for url()

Try l($performer, $url, array('query' => array('format' => 'simple'), /*...the rest of your array...*/ );
This is for drupal 7. In the options array you can specify a query array which is a key-value array of things to append to the url query string.

Related

How to echo PHP variable in action hook?

Here's my code:
<?php
use WHMCS\View\Menu\Item as MenuItem;
add_hook('ClientAreaPrimarySidebar', 1, function(MenuItem $primarySidebar)
{
if (!is_null($primarySidebar->getChild('Service Details Actions'))) {
$primarySidebar->getChild('Service Details Actions')
->addChild('Check', array(
'label' => 'Checker',
'uri' => 'http://example.com/check/'.print_r($vars['params']['domain']).'-check',
'order' => '3',
));
}
});
I am wanting the uri to link to here (assuming $domain = testdomain.com):
http://example.com/check/testdomain.com-check
Instead, it is showing this right now:
http://example.com/check/1-check
This is the specific line that I guess I'm having trouble with:
'uri' => 'http://example.com/check/'.print_r($vars['params']['domain']).'-check',
What am I doing wrong here?
The call to print_r() is unnecessary. Assuming that the string "testdomain.com" is stored in $vars['params']['domain'], then you simply need to concatenate the variable with the string like this:
'uri' => 'http://example.com/check/'.$vars['params']['domain'].'-check',
The function print_r() prints readable information about the given variable. Typically it is only used for debugging.

Having values in array that need to be output as string that will be used as code

I'm not sure that i've written something intelligibile in the title either.
I'll try to explain with some codes line. First some information.
I'm working with CakePhP and this problem comes up while creating the arrays for the actions allowed.
Long story short, i'm using an ACL to check whenever a page is loaded if the current user has access to the actions available on that page. This is an example:
//Controller
$role = $this->User->Role;
$role->id = $this->Auth->user('Role.id');
$actionsMenu = array();
$actionsMenu['Files'] = array(
'label' => 'Manage Files',
'controller' => 'project_files',
'action' => 'index',
'parameters' => $id,
'authorized' => $this->Acl->check($role, 'ProjectFiles')
);
$actionsMenu['Groups'] = array(
'label' => 'Manage Groups',
'controller' => 'groups',
'action' => 'index',
'parameters' => $id,
'authorized' => $this->Acl->check($role, 'Groups')
);
In the view I just loop and if the "authorized" is set to true, i'll print the related button.
Now, the problem rise when i've more that one parameter to pass. This is another snippet that follows the one up there:
//Controller [following]
$this->Project->id = $id;
if ($this->Project->field('archived')) {
$actionsMenu['Archiviation'] = array(
'label' => 'Restore',
'controller' => 'projects',
'action' => 'archiviation',
'parameters' => array($id, 0),
'authorized' => $this->Acl->check($role, 'controllers/Projects/archiviation')
);
} else {
$actionsMenu['Archiviation'] = array(
'label' => 'Archive',
'controller' => 'projects',
'action' => 'archiviation',
'parameters' => array($id, 1),
'authorized' => $this->Acl->check($role, 'controllers/Projects/archiviation')
);
}
This is what you can find in the views:
//View
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('<- Projects Management'), array('action' => 'index')); ?></li>
<li> </li>
<?php foreach($actionsMenu as $action) : ?>
<?php if($action['authorized']) : ?>
<li><?php echo $this->Html->link(__($action['label']), array('controller' => $action['controller'], 'action' => $action['action'],
is_array($action['parameters']) ? implode(', ', $action['parameters']) : $action['parameters']
)); ?></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
I thought the array format was the most CakePhP friendly way to pass the values just to discover that in case of multiple parameters, cake tend to prefer an associative array.
The problem here is that CakePhP read that implode as a whole parameter composed by a string.
This is an example:
<!--HTML-->
Restore
What I want to achieve is that the values separated by the comma should be read as different variables.
Honestly I never run in a situation like this and I've neither idea of what to look for on google to find something fitting (being a non-native english speaking isn't helping) so any suggestion is welcome.
Edit1
To clarify, the code listed in the second box (starting with Controller [following]) is the one that is causing problems.
The previous box just build the array with a single parameters that match the simple structure of CakePhP for building link but the second block will need to pass a second parameter. If the values was static one could simply type something like this:
//CakePhP HTML::Link
echo $this->Html->link(
'text of link',
array(
'controller' => 'controller_name',
'action' => 'action_name',
'parameter1', 'parameter2'
));
If I send the list of parameters as string, Cake are considering them a single parameter, reading it like (string)("'parameter1', 'parameter2'").
The same happens with the code i've written above in which i'm converting the array of values to string with implode().
So what I'm asking for, is if there a function, option or practice that I'm missing.
Edit2
As the user user221931 pointed out, the CakePhP function should support multiple parameters as array in the form of array('parameter1', 'parameter2', 'paramenterN').
So i've tried just removing the is_array() check and simply pass the value of $action['parameters']. The view section now look like this:
<?php echo $this->Html->link(__($action['label']), array(
'controller' => $action['controller'],
'action' => $action['action'],
//is_array($action['parameters']) ? implode(', ', $action['parameters']) : $action['parameters']
$action['parameters']
)); ?>
But i've got a warning as follows:
rawurlencode() expects parameter 1 to be string, array given
Going to open the context of the warning, seems like CakePhP is reading the information provided as follows:
$params = array(
'controller' => 'projects',
'action' => 'archiviation',
'plugin' => null,
'pass' => array(
(int) 0 => array(
(int) 0 => '1',
(int) 1 => (int) 1
)
),
'named' => array()
)
Honestly I'm lost here.
I've tried changing the second value of the array to a string too and passing an associative array instead of a numeric just to try something obvious but the error persist and the link comes out truncated without parameters.
The correct format to use Html::link()
echo $this->Html->link(
'text of link',
array(
'controller' => 'users', //Or any controller name
'action' => 'view', //Or any action
1, //Several parameters
'test1', //go here
'test2'
)
);
If you don't know the number of parameters beforehand, you only need to array_merge your fixed array with the array of dynamic parameters.
Assuming $arrayOfParameters holds array('test1', 'test2', 'test3')
$urlArray = array_merge(
array('controller' => 'users', 'action' => 'view'),
$arrayOfParameters
);
echo $this->Html->link('text of link', $urlArray);
Additionally you could implode your array of parameters as a url, i.e:
$urlString = implode('/', $arrayOfParameters); //creates a string 'test1/test2/test3'
echo $this->Html->link('text of link', array(
'controller' => 'users',
'action' => 'view',
$urlString //One parameter that will be looking as many
));

Not able to fetch Get params in production

I have developed API's with Zf2 and DynamoDB, I am able to get values from GET params in my local machine but unable to get values from GET params when I uploaded the API's in production
.
FYI: POST method is working properly in production.
Below is the controller get function.
public function get($id)
{
$abcModel = new ABCModel();
error_log("tournamentId:".$this->params()->fromQuery('tournamentId') );
$query = $this->getRequest()->getQuery();
error_log("tournamentId1:".$query['tournamentId']);
error_log("tournamentId2:".$this->getEvent()->getRouteMatch()->getParam('tournamentId'));
error_log("tournamentId3:".$this->params('tournamentId'));
error_log("tournamentId4:".$this->params()->fromRoute('tournamentId'));
}
I have tried all the answers of this question ZF2: Get url parameters in controller.
Can any one know what could be the reason for this?
Any light on the path would be helpful.
To use query string in the production environment, you have to use some alternate approach.
You can add parameter along with route to hold the query string value. But the query string needs to be passed using "/" mark between the route and query string instead of using "?" mark.
/route-name/key=value&key=value1
and the routing configuration need to be
'router' => array(
'routes' => array(
'<route-name>' => array(
'type' => 'segment',
'options' => array(
'route' => '<route-name>[/:action][/:queryPara][/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'queryPara' => '[a-zA-Z][a-zA-Z0-9_-&=]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
)
),
),
)),
You can create a function that will extract the query string and returns the array containing key=>value pairs of query string.
And in controller you have to call the function by passing the query string which will be stored in "/queryPara" part after the route name using the following statement you will get the string
$this->params('queryPara')
Hope it helps
Thanks

Exploding array for url

I'm developing a site for analyzing a store's data.
I need the url part of my array to look like this:
array(
'url' => 'http://some.website.com:8080/SASStoredProcess/do?_username=user-123',
'_password' => 'passwd',
'_program' => '/Utilisateurs/DARTIES3-2012/Mon dossier/analyse_dc',
'annee' => '2012',
'ind' => 'V',
'_action' => 'execute'
);
I currently have this and am struggling to convert it to the desired format:
array(
'url' => 'url=http://some.website.com:8080/SASStoredProcess/do?_username=user-123&_password=passwd&_program=%2FUtilisateurs%2FDARTIES3-2012%2FMon+dossier%2Fanalyse_dc&annee=2012&ind=V&_action=execute',
'otherKey' => 'otherValue'
);
Please can somebody help me to convert the URL in the second code block to look like the first code block? Thanks in advance.
So this will extract the url in the form you want, as $url:
$myArray = array(
'url' => 'url=http://some.website.com:8080/SASStoredProcess/do?_username=user-123&_password=passwd&_program=%2FUtilisateurs%2FDARTIES3-2012%2FMon+dossier%2Fanalyse_dc&annee=2012&ind=V&_action=execute',
'otherKey' => 'otherValue'
);
parse_str($myArray['url']);
echo $url;
You will need to decide where it needs to go next and how you get it there.
You might want to use: parse_url() and parse_str() over your $array['url']

How do I access a routed parameter from inside a controller?

This is the route...
'panel-list' => array (
'type' => 'Segment',
'options' => array (
'route' => '/panel-list/:pageId[/:action]',
'constraints' => array (
'pageId' => '[a-z0-9_-]+'
),
'defaults' => array (
'action' => 'index',
'controller' => 'PanelList',
'site' => null
),
),
),
What do I need to put in here....
public function indexAction()
{
echo ???????
}
to echo the pageId?
In beta5 of zf2 it changed to easier usage so you don't need to remember different syntax for every different type. I cite:
New "Params" controller plugin. Allows retrieving query, post,
cookie, header, and route parameters. Usage is
$this->params()->fromQuery($name, $default).
So to get a parameter from the route, all you need to do is.
$param = $this->params()->fromRoute('pageId');
This can also be done with query ($_GET) and post ($_POST) etc. as the citation says.
$param = $this->params()->fromQuery('pageId');
// will match someurl?pageId=33
$param = $this->params()->fromPost('pageId');
// will match something with the name pageId from a form.
// You can also set a default value, if it's empty.
$param = $this->params()->fromRoute('key', 'defaultvalue');
Example:
$param = $this->params()->fromQuery('pageId', 55);
if the url is someurl?pageId=33 $param will hold the value 33.
if the url doesn't have ?pageId $param will hold the value 55
Have you tried
$this->getRequest()->getParam('pageId')
$this->getEvent()->getRouteMatch()->getParam('pageId');

Categories