I'm working on a system where we want to queue up a batch of emails and then send them using a queue (so I can't use \Zend\Mail directly). The goal is to not have HTML inside the models that are creating the emails but instead have them in a view file. I'm trying to call the partial view helper inside the models (which I did in ZF1) but can't find anything on the web on how to do this. Is this possible in ZF2 or is there a better way? I've managed it before where we've had the HTML inside the models and it's been a mess. :-)
You can just create a template somewhere, pull the renderer from your service locator, and render away:
<?php
// this assumes you're in some AbstractActionController,
// but you could be in some service class that has the
// renderer injected, etc.
// template vars
$vars = array(/* .... */);
// create a ViewModel
$vm = new \Zend\View\Model\ViewModel($vars);
// path to your email template, compatible with your template map config.
$tpl = 'mymodule/emails/some-email-template.phtml';
$vm->setTemplate($tpl);
// get the renderer from the ServiceManager, so it's all nicely configured.
$renderer = $this->getServiceLocator()->get('Zend\View\Renderer\RendererInterface');
// render some HTML
$html = $renderer->render($vm);
You could do it this way:
1. In the module.config define those HTML mail content template views.
'view_manager' => array(
(...)
'template_map' => array(
'mail_content_1' => __DIR__ . '/../view/<MODULE_NAME>/mail_content_1.phtml',
),
(...)
)
2. Load the Template View in your model and get the HTML.
use Zend\View\Model\ViewModel;
public function getMailHTML() {
$view = new ViewModel;
$variables = array( "parm1" => "value1" );
//SET THE VARIABLES, LIKE IN PARTIALS
$view->setVariables( $variables );
//SET THE TEMPLATE VIEW KEY DEFINED IN THE module.config
$view->setTemplate( 'mail_content_1' );
//YOU'LL HAVE TO BE ABLE TO USE THE ServiceManager IN YOUR MAIL MODELS.
//RENDERING THE VIEW YOU'LL GET THE HTML
return $this->serviceManager->get( 'viewrenderer' )->render( $view );
}
Related
I want to embed a contact form in multiple places on my website.
I developed a contact form in a contact() function within my MessagesController.php:
// MessagesController.php
public function contact()
{
$this->set('title', 'Contact');
$message = $this->Messages->newEntity();
... // shortened for brevity
$this->set(compact('message'));
$this->set('_serialize', ['message']);
}
I loaded the CSRF component in the initialize() function of the AppController.php:
// AppController.php
public function initialize()
{
parent::initialize();
$this->loadComponent('Csrf');
... // shortened for brevity
}
The form is rendered with a contact.ctp and it works fine.
I followed CakePHP's cookbook which suggests using requestAction() within an element, then echoing the element where I want it:
// contact_form.ctp
<?php
echo $this->requestAction(
['controller' => 'Messages', 'action' => 'contact']
);
?>
And:
// home.ctp
<?= $this->element('contact_form'); ?>
The problem is that the form is rendered fine, but the CSRF hidden field is missing. It should be automatically added to the form since the CSRF component is called in the AppController.php.
I guess either using an element with a requestAction() isn't the solution for this particular case, or I am doing something wrong.
Any ideas? Thanks in advance for the input!
Request parameters need to be passed manually
requestAction() uses a new \Cake\Network\Request instance, and it doesn't pass the _Token and _csrf parameters to it, so that's why things break.
While you could pass them yourself via the $extra argument, like
$this->requestAction(
['controller' => 'Messages', 'action' => 'contact'],
[
'_Token' => $this->request->param('_Token'),
'_csrf' => $this->request->param('_csrf')
]
);
Use a cell instead
I would suggest using a cell instead, which is way more lightweight than requesting an action, also it operates in the current request and thus will work with the CSRF component out of the box.
You'd pretty much just need to copy your controller action code (as far as the code is concerned that you are showing), and add a loadModel() call to load the Messages table, something like
src/View/Cell/ContactFormCell.php
namespace App\View\Cell;
use Cake\View\Cell;
class ContactFormCell extends Cell
{
public function display()
{
$this->loadModel('Messages');
$this->set('title', 'Contact');
$message = $this->Messages->newEntity();
// ... shortened for brevity
$this->set(compact('message'));
$this->set('_serialize', ['message']);
}
}
Create the form in the corresponding cell template
src/Template/Cell/ContactForm/display.ctp
<?php
echo $this->Form->create(
/* ... */,
// The URL needs to be set explicitly, as the form is being
// created in the context of the current request
['url' => ['controller' => 'Messages', 'action' => 'contact']]
);
// ...
And then wherever you want to place the form, just use <?= $this->cell('ContactForm') ?>.
See also
API > \Cake\Routing\RequestActionTrait::requestAction()
Cookbook > Views > Cells
I wanted to have a contact-form-block that i can reuse on different pages and templates. So i decided to write a Twig extension. The problem is that i cant access the createFormBuilder() function. The second problem will be then that i cant access the request object for validation. My current code looks like this:
<?php
namespace Name\NameBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class ContactExtension extends \Twig_Extension
{
function getName() {
return 'contact_extension';
}
function getFunctions() {
return array(
'contactform' => new \Twig_Function_Method($this, 'contactform'),
);
}
function contactform() {
$form = $this->createFormBuilder()
->add('Name', 'text')
->add('Message', 'textarea')
->add('Send', 'submit')
->getForm();
return $this->render('NameBundle:forms:contactform.html.twig', array(
'form' => $form->createView(),
}
}
But i get error "Call to undefined method createFormBuilder()"...
Also i will get error if i change the function to function contactform(Request $request) { ... }
What do i need to add to use this function an object? Or maybe the twig extension is the completely wrong approach?
createFormBuilder() is a Controller helper that allows you to access the form.factory service within your controllers through the container (code below)
namespace Symfony\Bundle\FrameworkBundle\Controller;
// ...
class Controller extends ContainerAware
{
// ...
public function createFormBuilder($data = null, array $options = array())
{
return $this->container->get('form.factory')->createBuilder('form', $data, $options);
}
You're not in a "Controller context" here, so if you want to use the form.factory service within your extension you've to inject it.
BUT,
I'll not advice your to manage your contactForm this way (using a Twig Extension function). Why don't you just create a contactAction within the appropriate controller. You can then render your form in your templates using the twig render helper,
{{ render(controller('YourBundle:YourController:contactAction')) }}
If you use Symfony to make your code clear you should make Forms (src/forms) in diffrent file and just call it in your view and controller.
I would like to use as much standard TYPO3 as possible to create a form to edit single records from tx_mytable.
In pi1 i load the tca for the table:
t3lib_div::loadTCA('tx_mytable');
Now I would like to use standard functions to create my form elements more or less like it is done in the backend...
I found this for the front end but cannot find any working examples:
t3lib_TCEforms_fe.php (that extends the normal t3lib_TCEforms)
Is this the right way to go or is there a better way?
I got something working but not really that nice code in the frontend
Here is a link that telss that TCA is not enough but two new entries in the array is needed
http://www.martin-helmich.de/?p=15
It is itemFormElName and itemFormElValue
// include tceforms_fe (place outside class where pipase is included)
require_once(PATH_t3lib.'class.t3lib_tceforms_fe.php');
// load TCA for table in frontend
t3lib_div::loadTCA('tx_ogcrmdb_tasks');
// init tceforms
$this->tceforms = t3lib_div::makeInstance("t3lib_TCEforms_FE");
$this->tceforms->initDefaultBEMode(); // is needed ??
$this->tceforms->backPath = $GLOBALS['BACK_PATH']; // is empty... may not be needed
//////////REPEAT FOR EACH INPUT FIELD/////////
// start create input fields, here just a single select for responsible
// conf used for tceforms similar to but not exactly like normal TCA
$conftest = array(
'itemFormElName' => $GLOBALS['TCA']['tx_ogcrmdb_tasks']['columns']['responsible']['label'],
'itemFormElValue' => 1,
'fieldConf' => array(
'config' => $GLOBALS['TCA']['tx_ogcrmdb_tasks']['columns']['responsible']['config']
)
);
// create input field
$this->content .= $this->tceforms->getSingleField_SW('','',array(),$conftest);
// wrap in form
$output = '<form action="" name="editform" method="post">';
$output .= $this->content;
$output .= '</form>';
// wrap and return output
return $output;
Still looking for a working example with custem template for input fields.
I have a zend xml config like so:
<design>
<navigation>
<frontend>
<company>
<label>Company</label>
<route>sitepage</route>
<pages>
<about>
<label>About us</label>
<route>sitepage</route>
<params>
<page>about-us</page>
<language>en</language>
</params>
</about>
Here is my sitepage route:
resources.router.routes.sitepage.type = Zend_Controller_Router_Route
resources.router.routes.sitepage.route = ":language/page/:page"
resources.router.routes.sitepage.defaults.module ="core"
resources.router.routes.sitepage.defaults.controller = "page"
resources.router.routes.sitepage.defaults.action = "view"
resources.router.routes.sitepage.defaults.page = "home"
resources.router.routes.sitepage.defaults.language = "en"
As you can see, what I do is set the page param within the <params> xml node. I tried adding the <language> parameter thinking it would automatically update to the application language, but it doesnt seem to work that way. My navigation menu just outputs, for example, http://localhost/en/page/about-us when It should be http://localhost/it/page/about-us (given that my application is registered as using the it language). How can I get my navigation to recognize the application language (it) ?
I worked this out a different way...
Here is an example for a main registration page:
routes.register_index.route = #lang/#register
routes.register_index.defaults.controller = register
routes.register_index.defaults.action = index
My navigation.xml is empty but you could use just a label and route.
You may have noticed # instead of : in my routes. This is designed mostly for i18n sites.
In my language folder, I have the regular en.php, etc. AND url-en.php, etc.
Here is what it looks like:
<?php
return array(
'lang' => 'en',
'register' => 'register',
'about' => 'about-us',
);
With this, the same route will be used for /en/register, /fr/inscription, etc. (one for each language file)
In my Bootstrap, I include these with the Rewrite initiation:
protected function _initRewrite()
{
$translator = new Zend_Translate('array', APPLICATION_PATH . '/language/url-fr.php', 'fr');
$translator->addTranslation(APPLICATION_PATH . '/language/url-en.php', 'en');
// Set the current locale for the translator
$locale = Zend_Registry::get('Zend_Locale');
$translator->setLocale($locale);
// Set it as default translator for routes
Zend_Controller_Router_Route::setDefaultTranslator($translator);
$front_controller = Zend_Controller_Front::getInstance();
$router = $front_controller->getRouter();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', APPLICATION_ENV);
$router->addConfig($config, 'routes');
$router->addDefaultRoutes();
}
This basically tells your application to translate #lang by its value from url-lang.php according to the current locale. Locale has to be set before this point in your bootstrap. You can either retrieve it from session, cookie, URL, anything.
Once this is done, you can call your route anywhere and it'll be in the current locale, using data from the special language files. Make sure your nav file has the same names as your routes and you should be fine.
By default you have the following URL-syntax in ZF: /module/controller/action. What i want, is to build an menu-system where i can use any URL I want.
Lets say I make an menu-item called 'news'. When i call http://www.site.com/news i want to have the folowing loaded:
module: news
controller: frontpage
action: display
These config-values must be configured in the database-record for the menu-item.
How can I do this in zend? I spend a lot of time searching for it, but I still can't figure out how to. Does anybody?
I'd suggest using a front controller plugin to scan your database for all the entries, create routing rules based on those entries and add them to the router (see this).
Of course caching strategy is recommended so that you don't do a lot of processing on every request.
You can create a plugin and in routeStartup define something that intercept your request and route /module/controller/action to /action, but for this all your action names must be unique :
class My_CustomRouterPlugin extends Zend_Controller_Plugin_Abstract
{
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$fc = Zend_Controller_Front::getInstance();
$action =$fc->getRequest()->getActionName();
$router = $fc->getRouter();
$model= new myModel();
$myPage = $model->getPageByAction($action);
$route = new Zend_Controller_Router_Route('/action', array(
'module' => $myPage->getModule();
'controller' => $myPage->getController();
'action' => $action;
));
$router->addRoute($action, $route);
return $router;
}
}
In myModel define a method can get you an object(or an array) that contains module, controller names (from you DB ).
and register this plugin in your bootstrap:
$front->registerPlugin(new My_CustomRouterPlugin());