Creare automaticamente con Symfony un Form per accedere ai dati del dababase.
In Symfony un progetto è composto da applicazioni suddivise in moduli, ad esempio:
– jobeet/apps/frontend/modules/contenuto
– jobeet/apps/frontend/modules/job
– jobeet/apps/frontend/modules/nome altro modulo …
– jobeet/apps/nome altra app…
1. Generiamo automaticamente il codice php per la gestione di un modulo di nome ‘job’
cmd.exe -> C:\wamp64\www\jobeet>symfony doctrine:generate-module –with-show –non-verbose-templates frontend job JobeetJob
verrà creato automaticamente:
apps/frontend/modules/job/actions/
apps/frontend/modules/job/templates/
in particolare apps/frontend/modules/job/actions/action.class.php
<?php /** * job actions. * * @package symfony * @subpackage job * @author Your name here * @version SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */ class jobActions extends sfActions { public function executeIndex(sfWebRequest $request) { $this->jobeet_jobs = Doctrine_Core::getTable('JobeetJob') ->createQuery('a') ->execute(); } public function executeShow(sfWebRequest $request) { $this->jobeet_job = Doctrine_Core::getTable('JobeetJob')->find(array($request->getParameter('id'))); $this->forward404Unless($this->jobeet_job); } public function executeNew(sfWebRequest $request) { $this->form = new JobeetJobForm(); } public function executeCreate(sfWebRequest $request) { $this->forward404Unless($request->isMethod(sfRequest::POST)); $this->form = new JobeetJobForm(); $this->processForm($request, $this->form); $this->setTemplate('new'); } public function executeEdit(sfWebRequest $request) { $this->forward404Unless($jobeet_job = Doctrine_Core::getTable('JobeetJob')->find(array($request->getParameter('id'))), sprintf('Object jobeet_job does not exist (%s).', $request->getParameter('id'))); $this->form = new JobeetJobForm($jobeet_job); } public function executeUpdate(sfWebRequest $request) { $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT)); $this->forward404Unless($jobeet_job = Doctrine_Core::getTable('JobeetJob')->find(array($request->getParameter('id'))), sprintf('Object jobeet_job does not exist (%s).', $request->getParameter('id'))); $this->form = new JobeetJobForm($jobeet_job); $this->processForm($request, $this->form); $this->setTemplate('edit'); } public function executeDelete(sfWebRequest $request) { $request->checkCSRFProtection(); $this->forward404Unless($jobeet_job = Doctrine_Core::getTable('JobeetJob')->find(array($request->getParameter('id'))), sprintf('Object jobeet_job does not exist (%s).', $request->getParameter('id'))); $jobeet_job->delete(); $this->redirect('job/index'); } protected function processForm(sfWebRequest $request, sfForm $form) { $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); if ($form->isValid()) { $jobeet_job = $form->save(); $this->redirect('job/edit?id='.$jobeet_job->get()); } } }
Puntare il browser a: http://localhost/jobeet/web/index.php/job per vedere il form in azione
2. Personalizziamo il template a: jobeet/apps/frontend/templates/layout.php
<!-- apps/frontend/templates/layout.php --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Jobeet - Your best job board</title> <link rel="shortcut icon" href="/favicon.ico" /> <?php include_javascripts() ?> <?php include_stylesheets() ?> </head> <body> <div id="container"> <div id="header"> <div class="content"> <h1><a href="<?php echo url_for('job/index') ?>"> <img src="/legacy/images/logo.jpg" alt="Jobeet Job Board" /> </a></h1> <div id="sub_header"> <div class="post"> <h2>Ask for people</h2> <div> <a href="<?php echo url_for('job/index') ?>">Post a Job</a> </div> </div> <div class="search"> <h2>Ask for a job</h2> <form action="" method="get"> <input type="text" name="keywords" id="search_keywords" /> <input type="submit" value="search" /> <div class="help"> Enter some keywords (city, country, position, ...) </div> </form> </div> </div> </div> </div> <div id="content"> <?php if ($sf_user->hasFlash('notice')): ?> <div class="flash_notice"> <?php echo $sf_user->getFlash('notice') ?> </div> <?php endif; ?> <?php if ($sf_user->hasFlash('error')): ?> <div class="flash_notice"> <?php echo $sf_user->getFlash('error') ?> </div> <?php endif; ?> <div class="content"> <?php echo $sf_content ?> </div> </div> <div id="footer"> <div class="content"> <span class="symfony"> <img src="/legacy/images/jobeet-mini.png" /> powered by <a href="/"> <img src="/legacy/images/symfony.gif" alt="symfony framework" /> </a> </span> <ul> <li><a href="">About Jobeet</a></li> <li class="feed"><a href="">Full feed</a></li> <li><a href="">Jobeet API</a></li> <li class="last"><a href="">Affiliates</a></li> </ul> </div> </div> </div> </body> </html>
Incompatibilità PHP 5.5
Puntare il browser a: http://localhost/jobeet/web/frontend_dev.php/job
preg_replace() è deprecata, andrebbe sostituita nelle librerie Symfony con preg_replace_callback(), cancelliamo i warning testuali generati nel codice e tentiamo di correggere l’errore (https://gist.github.com/gitllermopalafox/0a8b60eaafede6af0b75)
Correggere C:\wamp64\bin\php\php5.6.25\pear\symfony\response\sfWebResponse.class.php riga 409
protected function normalizeHeaderName($name) { // return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", strtr(ucfirst(strtolower($name)), '_', '-')); return preg_replace_callback( '/\-(.)/', function ($matches) { return '-'.strtoupper($matches[1]); }, strtr(ucfirst(strtolower($name)), '_', '-') ); }
Correggere C:\wamp64\bin\php\php5.6.25\pear\symfony\util\sfToolkit.class.php riga 362:
public static function pregtr($search, $replacePairs) { // return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search); foreach($replacePairs as $pattern => $replacement) $search = preg_replace_callback( $pattern, function ($matches) use ($replacement){ if(array_key_exists(1, $matches)){ $replacement = str_replace("\\1", $matches[1], $replacement);} if(array_key_exists(2, $matches)){ $replacement = str_replace("\\2", $matches[2], $replacement);} return $replacement; }, $search ); return $search; }