Laminas toRoute as POST? - php

I work with Laminas and in a ControllerAction function I want to redirect to another url as a post by using $this->redirect()->toRoute('newSite', $noURLParams, $options);
When I use the "query"-Option ($options = [$query => ['postPara1' => 'blabla', 'postPara2' => 'blubblub']]), I will get the "post"-Parameter as GET-Parameter in the URL:
hhttp://localhost/new-site?postPara1=blabla&postPara2=blubblub
So everyone could see the content of the parameters.
Is there a way to get the toRoute() to make a "POST" out of it?
Or do I have to use another function for this?

Thank you for your interest and help. After you said that it doesn't work, I changed the sequence of what, when should be called and now I can do it without an additional redirect.

Related

In Slim, how do I pass a GET variable to render

I've only been working with Slim for a short while. I'm still on version 2. So far everything has been going just fine, but I've hit a little snag. I have a page that displays content based on a GET variable at the end of the URL. The url looks like the following...
http://localhost/trailcache.com/checklist/21
The first line of the get route looks like this...
$app->get('/checklist/:Id', function($Id) use($app) {
It ends like this...
})->name('checklist');
That Id parameter controls what info I'm pulling into the page and everything has been going just fine, but now I've added a contact form and with it, some validation. I'm writing to the DB and rendering the new content okay. The problem arises when I try to send errors back to the page. Currently it looks like this...
$app->render('user/checklist.php', [
'Id' => $Id,
'errors' => $v->errors(),
'request' => $request
])->name('checklist');
This doesn't work. The page is blank. The url it returns is...
http://localhost/trailcache.com/checklist
The documentation for render shows...
$app->get('/books/:id', function ($id) use ($app) {
$app->render('myTemplate.php', array('id' => $id));
});
Wouldn't that work in the post route the same way? It has on all my other pages.
How can it get pass those errors along with the correct GET variable so that the correct content displays?
This works for me just fine.
return $this->renderer->render('myTemplate.php', array('id' => $id));
Hope it works for you.
After a few days of struggling with this, I found a solution. I first discovered map() and changed the first line to...
$app->map('/checklist/:Id', function($Id) use($app) {
and the end to...
})->via('GET', 'POST')->name('checklist');
so now the route renders the same whether it's coming from post or get. I then pointed the form to this named route...
<form action="{{ urlFor('checklist', {Id: Id}) }}" class="form" method="post">
and moved all the post logic inside this route. Since there is no need to validate if the request wasn't a post I used...
if($app->request->isPost()) {
and put the validation code inside. Refreshing the page after making a form submission would resubmit the form so if validation passed I added...
return $app->response->redirect($app->urlFor('checklist', array(
'Id' => $Id
)));
All I had to do was pass the errors to the template and it seems to be working perfectly.

Cleanest way to include urls in Moodle emails

I'd like to know which is the cleanest way to insert an url in an email sent by Moodle module.
So far I'm using this formula, what IMHO I don't think is the cleanest way:
$url = $CFG->wwwroot.'/mod/<mymodulename>/view.php?id='.$cm->id;
The things I don't like here are:
Using $CFG->wwwroot
/mod/<mymodulename> needs to be provided always. (Assume here that I'm using a constant instead of a hardcoded string).
I expected Moodle to have a function to provide this out of the box just when providing module script. I've tried moodle_url but this function doesn't provide the path to the php script when used this way:
new moodle_url('view.php?id='.$cm->id);
I just get:
view.php?id=XX
Thanks in advance.
I would do it like this
$url = new moodle_url('/mod/<mymodulename>/view.php', array('id' => $cm->id));
echo html_writer::link($url, get_string('linktitle', 'mod_mymodulename'));
You can use following statement:
This is Absolute path of file
$url = new moodle_url($CFG->wwwroot.'/mod//view.php', array('id' => $cm->id));

(#100) At least one reference object must be specified - Simple POST of OG action

I am using the PHP SDK to try and post a simple action using the Graph API.
I have set up an object called 'Competition' and an action called 'Enter'. As such, a user enters a competition.
My code is like so:
try {
$statusUpdate = $this->data['facebook']->api('/me/lovepoppycompetition:enter', 'post', array(
'competition' => 'https://apps.facebook.com/lovepoppycompetition/'
));
echo '<pre>SUCCESS: '.print_r($statusUpdate, true).'</pre>';
} catch(FacebookApiException $e) {
echo '<pre>FAILED: '.print_r($e, true).'</pre>';
}
I am getting an error returned (FacebookApiException):
(#100) At least one reference object must be specified
I'm not really sure why, especially as I am using this code (with different objects/actions) on another app with no issues just fine.
It might be worth noting that I haven't submitted the action, although I don't think I have to for it to work for me?
Thanks in advance.
I think the problem lies with you redirecting the action back to your facebook application. You should create a competition page on your website, and use that to redirect users back to your website.
Make sure you use the correct og tags on the redirect page.
This was caused by a FB bug, which is now resolved

Do query strings need to be manually sent to POST requests

This is a CakePHP / General PHP question.
In my application I use a query string like /login?continue=/admin/posts
This query string is used to redirect users to the URL in the query, but it doesn't work so it seems as though the app can't see the string...
This has got me wondering as basically when you arrive at the page with the string it's a GET request where as when you login, it becomes a POST or XML request (if using AJAX). Do I need to add the query string manually to the form for the POST to see it?
Either in the form action or a hidden input? Or am I barking up the wrong tree?
I'm currently grabbing the query like so:
if(isset($this->params['url']['continue']))
{
$pathtoredirect = $this->params['url']['continue'];
}
else
{
$pathtoredirect = $this->Auth->redirect();
}
But that's within the POST request so perhaps the query is lost... and adding it to a hidden input would not solve the problem with the current code so I would either change the code to look at the hidden field or pass the query with the action on the form?
e.g. <form action="/login?continue=/admin/posts" method="post">
Am I correct in thinking this? And would anyone be able to offer solutions or pros and cons of the two methods I mention?
In short I'm asking how to add the query string to my form action value
It currently looks like:
php echo $this->Form->create('User',
array(
'id' => 'loginform',
'type' => 'post',
'url' => array
(
'admin'=>false,
'controller' => 'users',
'action' => 'login'
)
)
);
So how would I add the query string to the form?
Thanks
When receiving a POST request you can receive both POST and GET variables through the superglobals $_POST and $_GET.
You can either send your paramater in $_GET by including it in the form's action attribute or send it in $_POST by creating an <input type="hidden"> tag within the form
A slash (/) is a reserved character. Encode it with %2F.
http://blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy
The solution was to do this:
<?php echo $this->Form->create('User',
array('id' => 'loginform', 'type' => 'post',
'url' => array('admin'=>false,'controller'=>'users','action'=>'login','url'=>$this->params['url']['continue']))); ?>
as I have a route already setup to handle the additional URL parameter:
Router::connect('/auth/login', array('controller'=>'users','action'=>'login'));
Router::connect('/auth/login?continue=:url',
array('controller'=>'users','action'=>'login'),
array(
'url' => '[A-Za-z0-9/\._-]+',
'pass' => array('url')
));
If anyone sees any issues with the way I do this though, please feel free to comment.

PHP how to pass URL parameters to create a new URL

Very newbie question: setting up a Meraki wireless EXCAP walled garden, and will have users land on a terms-of-service.php (simple checkbox)... upon submission, will land on page with other info THEN need to pass to open web. Need to grab first URL, save it, pass to page2.php, and then out to web.
Meraki's example of incoming URL (when user attempts to access wireless):
http://MyCompany.com/MerakiSplashPage/?base_grant_url=https://example.meraki.com/splash/grant&user_continue_url=http://www.google.com&node_id=222222&gateway_id=222222&client_ip=10.222.222.222
Then "When you are ready to grant access to the user, send the user to GET['base_grant_url'] + "?continue_url=" + GET['user_continue_url']. In the case of the example above, this URL would be:
https://example.meraki.com/splash/grant?continue_url=http://www.google.com
Going in circles on how to do this, any suggestions would be much appreciated.
Use rawurlencode to encode the value properly:
'http://MyCompany.com/MerakiSplashPage/?base_grant_url='.rawurlencode('https://example.meraki.com/splash/grant&user_continue_url=http://www.google.com').'&node_id=222222&gateway_id=222222&client_ip=10.222.222.222'
You can also use http_build_query to build the query automatically:
$query = array(
'base_grant_url' => 'https://example.meraki.com/splash/grant&user_continue_url=http://www.google.com',
'node_id' => '222222',
'gateway_id' => '222222',
'client_ip' => '10.222.222.222'
);
'http://MyCompany.com/MerakiSplashPage/?'.http_build_query($query)
Your final URL would be:
$_GET['base_grant_url']."?".$_GET['user_continue_url'];

Categories