Zend redirector with query string - zend-framework

I want to create a redirect using a query string in Zend 1.12. The optional parameters should be in the form of a query string.
This is my code:
if ($this->_request->getParam('partner')){
$controller = $this->getRequest()->getControllerName();
$action = $this->getRequest()->getActionName();
$module = $this->getRequest()->getModuleName();
$params = array(
"utm_source" => "affiliate",
"utm_medium" => "cpa",
"utm_term" => $this->_request->getParam('partner'),
"utm_campaign" => "partners",
"url" => $this->_request->getParam('url')
);¬
$this->_helper->redirector($action, $controller, $module, $params);
return false;
}
}
This produces an URL like
/content/agb/utm_source/affiliate/utm_medium/cpa/utm_term/foo
However I want this to look like:
/content/agb?utm_source=affiliate&utm_medium=cpa&utm_term=foo
How could I do that?
Thanks!

I've had better success with building the URL myself and using gotoUrlAndExit()
$this->_helper->redirector->gotoUrlAndExit('place?thing=value');

Related

Dynamic facebook app credentials for Laravel 5.1 socialite

We are trying to use dynamic facebook app credentials for Laravel 5.1 socialite.
config/services.php
'facebook' => [
'client_id' => 'xxxx',
'client_secret' => 'xxxx',
'redirect' => 'http://example.com/facebook-callback',
],
On my controller file:
public function getConnectFacebook()
{
return Socialite::driver('facebook')->redirect();
}
public function getFacebookCallback()
{
$user = Socialite::driver('facebook')->user();
}
We tried to overwrite the facebook driver this way:
public function getConnectFacebook()
{
Config::Set("services.facebook.client_id", "dynamic_app_id");
Config::Set("services.facebook.client_secret", "dynamic_app_secret");
Config::Set("services.facebook.redirect", "dynamic_app_redirect");
return Socialite::driver('facebook')->redirect();
}
But it was not working. Could you please let me know how we can achieve this?
Thanks.
like this
return Socialite::driver('facebook')->redirect()->setTargetUrl('your callback url');
Here is my solution.
private function makeFacebookDriver($domain){
//grab and set your config values from database or array. Don't do Config::Set.
$config['client_id'] = '';//grab fb id based from db based on domain
$config['client_secret'] = '';//grab fb secret from db based on domain
$config['redirect'] = 'http://'.$domain.'/fbcallback';
return Socialite::buildProvider(\Laravel\Socialite\Two\FacebookProvider::class, $config);
}
then use the function instead of calling Socialite::driver('Facebook');
$fb = $this->makeFacebookDriver('www.test.com');
return $fb->redirect();
just use it in your controller
use Laravel\Socialite\Two\FacebookProvider;
config
$config = [
'client_id' => '969935-d61celu1qck667krmbgql.apps.googlesercontent.com',
'client_secret' => 'sHrnnOz3Fmz4',
'redirect' => 'http://localhost:8000/api/login/facebook/callback'
];
$user= Socialite::buildProvider(FacebookProvider::class, $config)->stateless();
return $user->redirect();
stateless in callback
$userSocial =$config = [
'client_id' => '969d61celu1qck667krmbgql.apps.googlesercontent.com',
'client_secret' => 'sHrnnO3Fmz4',
'redirect' => 'http://localhost:8000/api/login/facebook/callback'
];
$user=Socialite::buildProvider(FacebookProvider::class, $config)-
>stateless()->user();
api or web.php
Route::get('login/{provider}', 'SocialController#redirect');
Route::get('login/{provider}/callback','SocialController#Callback');
You could also do a simple find and replace.
Let's say you have a config file like this
'facebook' => [
'client_id' => '{ID}',
'client_secret' => '{SECRET}',
'redirect' => '{REDIRECT}',
],
Now within your controller you can do something like this.
$fileName = 'path/to/file.php';
$configData = file_get_contents($file);
$configData = str_replace('{ID}','dynamic_id',$configData);
$configData = str_replace('{SECRET}','dynamic_secret',$configData);
$configData = str_replace('{REDIRECT}','dynamic_link',$configData);
file_put_contents($file, $configData);
That's it, nothing fancier.

Laravel 4 list and detail route

I want to achieve the following:
devices > Controller#devices
devices/{id} > Controller#devices
Is this possible with Laravel ? I'm trying to map a domotic box with an android application ImperiHome, and they expect me to have the same route for devices list and for any device action.
So far I've tried this:
Route::get('devices/{deviceId}/action/{actionName}/{actionParam?}', 'DomoticzController#devices');
Route::get('devices', 'DomoticzController#devices');
But I cannot retrieve the argument when I call the devices/id url
Ok, so to solve the php strict standard error I just splitted the routes to two methods as follows:
routes.php
Route::get('devices/{deviceId}/action/{actionName}/{actionParam?}', 'DomoticzController#device');
Route::get('devices', 'DomoticzController#devices');
Route::get('rooms', 'DomoticzController#rooms');
//Route::get('action_ret', 'DomoticzController#action_ret');
Route::get('system', 'DomoticzController#system');
Route::get('/', 'DomoticzController#system');
DomoticzController.php
/**
* Call for an action on the device identified by $deviceId.
* #return string Json formated action status.
*/
public function device($deviceId, $actionName, $actionParam = null)
{
$client = $this->getClient();
$request = $client->getClient()->createRequest('GET', get_url("json.htm?type=command&param={$actionName}&idx={$deviceId}}&switchcmd=$actionParam"));
$response = $request->send();
$input = $response->json();
// convert to app format
$output = array('success' => ('OK' === $input['status'] ? true : false), 'errormsg' => ('ERR' === $input['status'] ? 'An error occured' : ''));
return Response::json($output);
}
/**
* Retrieve the list of the available devices.
* #return string Json formatted devices list.
*/
public function devices()
{
$client = $this->getClient();
$request = $client->getClient()->createRequest('GET', get_url('json.htm?type=devices&used=true'));
$response = $request->send();
$input = $response->json();
// convert to app format
$output = new stdClass();
$output->devices = array();
foreach ($input['result'] as $device) {
$output->devices[] = array (
'id' => $device['idx'],
'name' => $device['Name'],
'type' => 'DevSwitch',
'room' => null,
'params' => array(),
);
}
return Response::json($output);
}
maybe there is a better way to solve this, I would be glad to hear it.
If you let both routes use the same controller action, you need to make the parameters optional in the controller I think.
Try this public function device($deviceId = null, $actionName = null, $actionParam = null) and see if you still get the PHP strict error.
You can not have a route without parameters be redirected to a controller action that expects parameters. You can, on the other hand, make a route with parameters be redirected to a controller action with optional parameters (this does not mean that your route parameters need to be optional).

Pass the current querystring parameters through symfony forms

Our application uses several webforms and some of them only should add or change several parameters in the querystring. As example there are a filter form and a form for the list order (just a dropdown).
Both are indipendant, but if I change one, I have to pass the current querystring parameters with the new request. How can I manage it?
This is just one of the possible solutions, it will depend on your implementation if it would help you.
<?php
$allowedQueryParams = array('param1', 'param2', 'param3');
// I'll fake a request here
$request = new Request(array('param2' => 'foo', 'param3' => 'bar', 'param4' => 'dummy'));
$formFactory = Forms::createFormFactoryBuilder()->getFormFactory();
$formBuilder = $formFactory->createBuilder();
$formBuilder->add('param1', 'text');
$data = array();
foreach ($allowedQueryParams as $param) {
if ($request->query->has($param)) {
// Add the query param as hidden field to you form
$formBuilder->add($param, 'hidden');
$data[$param] = $request->query->get($param);
}
}
$formBuilder->setData($data);
$form = $formBuilder->getForm();
So basically you add hidden fields with the current query parameters.
Edit:
But if you would have many forms, you might want to do this with an EventSubscriber
An alternative approach would be to combine the two forms into one master form.
From your controller:
$formData = array(
'filterParams' => array(), // Default filter parameters
'listOrderParams' => array(), // Default list order parameters
);
$form = $this->createFormBuilder($task)
->add('filterParams', new FilterFormType())
->add('listOrderParams', new ListOrderFormType())
->add('update', 'submit')
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$formData = $form->getData();

How to ensure several parameters validation in a Zend_Rest_Controller?

I need to make sure that several parameters are provided in my Zend_Rest_Controller.
Here is my code:
public function indexAction() {
$filters = array(
'locid' => array('HtmlEntities', 'StringTrim')
);
$validators = array(
'locid' => array('NotEmpty')
);
$input = new Zend_Filter_Input($filters, $validators);
$input->setData($this->getRequest()->getParams());
if($input->isValid())
{
echo "Correct";
}
else
{
echo "missing/invalid params";
}
}
But if I supply the url formated like this (without query string):
localhost/ws
it returns 'Correct', instead of 'missing/invalid params'.
Any easy solution or parameters to include in validators?
Thanks.
I think that behavior is correct. The servername: 'localhost' in your case must be provided to call the actual validator

Output Zend Form To Array of Key => Value

I would like a Zend_Form object to be output as essentially
array('name' => 'input username in db', 'description' => 'description thats in the database');
so far If I do
print_r((array) $form);
I get:
{"\u0000*\u0000_attribs":{"name":"add","enctype":"multipart\/form-data","method":"post"},"\u0000*\u0000_decorators":{"FormElements":{"decorator":"FormElements","options":null},"HtmlTag":{"decorator":"HtmlTag","options":{"tag":"dl","class":"zend_form"}},"Form":{"decorator":"Form","options":null}},"\u0000*\u0000_defaultDisplayGroupClass":"Zend_Form_DisplayGroup","\u0000*\u0000_description":null,"\u0000*\u0000_disableLoadDefaultDecorators":false,"\u0000*\u0000_displayGroupPrefixPaths":[],"\u0000*\u0000_displayGroups":[],"\u0000*\u0000_elementDecorators":null,"\u0000*\u0000_elementPrefixPaths":[],"\u0000*\u0000_elements":{"name":{"helper":"formText"},"name_url":{"helper":"formText"},"description":{"helper":"formTextarea","rows":"10"}
etcetc, alot of Zend storage
If I do:
$form = new Form_Administration_Movie_Add();
$elements = $form->getElements();
foreach($elements as $key => $element) {
echo $key;
}
I get a list of the fields, however I cannot do $element->getValue() as I just get 0 or 1 and no the actual data.
Ideas?
$array = $form->getValues();
Does this work? :)
Edit: You might need to use either $form->isValid($_POST) or $form->populate($_POST) before calling the getValues() method.