Post redirect get plugin

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Post redirect get plugin

Wermutstropfen
Hi there,

I have problems understanding the PRG-Plugin in the ZF2 documentation

// Pass in the route/url you want to redirect to after the POST
$prg = $this->prg('/user/register', true);

if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
    // returned a response to redirect us
    return $prg;
} elseif ($prg === false) {
    // this wasn't a POST request, but there were no params in the flash messenger
    // probably this is the first time the form was loaded
    return array('form' => $myForm);
}

// $prg is an array containing the POST params from the previous request
$form->setData($prg);

// ... your form processing code here  

This ist my action doing form processing

public function contactAction() {
   $contactForm = new ContactForm();
   $viewModel   = new ViewModel( array(
      'form' => $contactForm
   ) );

   $request = $this->getRequest();

   if ( $request->isPost() ) {
      $contactForm->setData( $request->getPost() );
      $this->smtpMailer( $contactForm )->mailFormData();

      return $viewModel;
   } else {
      return $viewModel;
   }
}  

I don't get it. How do I combine these two?

The rest of my code is as follows

 <?php

namespace ZendSkeletonModule\Model;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Form\Form;
use Zend\Mail\Message;

abstract class AbstractFormMailer extends AbstractPlugin {

    private $form;
    private $mail;
    private $transport;

    abstract protected function getTransport();

    function __invoke(Form $form) {
        $this->form = $form;
        $this->mail = new Message();
        $this->transport = $this->getTransport();

        return $this;
    }

    public function mailFormData() {
        $this->processFormData();
    }

    private function processFormData() {
        if ($this->form->isValid()) {
            $this->composeMail();
            $this->sendMail();
        }
    }

    private function composeMail() {
        $filteredFormData = $this->form->getInputFilter();

        $this->mail->addFrom($filteredFormData->getValue('email'));
        $this->mail->addTo('info@example.com');
       $this->mail->setSubject($filteredFormData->getValue('subject'));
        $this->mail->setBody($filteredFormData->getValue('body'));
    }

    private function sendMail() {
        if (!$this->isSpam()) {
            $this->transport->send($this->mail);
        }
    }

    private function isSpam() {
        if ($this->form->get('email2')->getValue()) {
            return true;
        };
    }

} 

 <?php

namespace ZendSkeletonModule\Controller\Plugin;

use ZendSkeletonModule\Model\AbstractFormMailer;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;

class SmtpMailer extends \ZendSkeletonModule\Model\AbstractFormMailer {

    public function getTransport() {
        $transport = new SmtpTransport();
        $options   = new SmtpOptions(array(
            'host'              => 'smtprelaypool.ispgateway.de',
            'connection_class'  => 'login',
            'connection_config' => array(
                'username' => 'info@example.com',
                'password' => 'xxx',
            ),
        ));
        $transport->setOptions($options);

        return $transport;
    }

} 
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Post redirect get plugin

Alejandro Celaya
I think you missed the code snippets. Anyway, the PRG is easy.

Depending on the type of the request, the plugin will return a different
type of data.
The first time you preform a GET request it will return false. In that
case, just render your view by, for example, returning a ViewModel.
If a POST request is performed, the plugin will save the post data in the
session and return a Response object. That object contains the Location
header and the 303 status code, so you just need to make your action to
return it (you know that a controller action may return Response objects)
That redirect will make the client to perform a new GET request, but now
the plugin sees there is data in the session and it will return it as an
array instead of false. In that case you need to process the data (with a
form object for example) and, again, render your view.

And that's all the process. You just need to check the returned data type
and do one thing or another depending on that.

An example is in the indexAction of this controller
https://github.com/acelaya/website/blob/master/module/Application/src/Controller/ContactController.php

In the future, if you have any doubts like this, you can ask in the ZF2
Facebook group http://www.facebook.com/groups/zendframework2/

--
Alejandro Celaya AlastruƩ
www.alejandrocelaya.com
El 16/10/2014 07:32, "Wermutstropfen" <[hidden email]> escribiĆ³:

> Hi there,
>
> I have problems understanding the PRG-Plugin in the ZF2 documentation
>
>
>
> This ist my action doing form processing
>
>
>
> I don't get it. How do I combine these two?
>
> The rest of my code is as follows
>
>
>
> [hidden email]
>
>
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/Post-redirect-get-plugin-tp4662329.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>