Here's the same stand-alone form class for Zend Framework 2. I am posting this script as-is from the Content Connection Kit so you'll need to modify it to remove errors of omission. The demo url is http://fhqk.com/cck/?zftest/zfform_factory.
Be aware that I have found that ZF2's take-over of session handling means that any use of session_start() in the container script will cause ZF2 to fail with fatal error. This means that the use of session_start() for other frameworks or class libraries like PFBC running in the same scope will block ZF2 from loading. Because of things like this incompatibility with other systems like Drupal 7.x< 8.x and Symfony2 can be expected.
You can work around the problem of ZF2 sessions by avoiding the use of captcha and other functionality that is dependant on sessions. But if anyone knows of a way to disable/override ZF2 session handling completely please speak up!
Popular just now
-
When dealing with Amazon Web Services using the command line for accessing the file system to configure the server using Putty is OK. But wh...
-
This was making me a little crazy today so I thought I would share. "Manual" PHP 5.5.x with WampServer Installing Windows (War...
-
One of my favorite pastimes is eavesdropping on programmers and developers while riding the commuter trains. You'd be amazed at the stuf...
ad1
Showing posts with label Zend Framework. Show all posts
Showing posts with label Zend Framework. Show all posts
How to Use Zend Framework 2 Forms in Another Script
I have been going back and forth on Zend Framework 2. First it helps me by providing some really robust code. The flip side of the coin is ZF2 makes me crazy because it's always explained terribly by classically educated computer scientist. Given that me and much of the PHP community are autodidacts this is a turn-off.
I always can tell how useful a PHP framework is by trying to do what I learned first in PHP, building a HTML form. Not one post or tutorial I could find gave a straight forward answer a to how things worked. What was worse no one gave working code for using ZF2 as a library used by another simple PH script. Trying to get this done simply and without Zends MVC in place seemed to be unattainable. This meant that ZF2 was not something I would use at first thought. That has changed now that I have this code working. This is a complete stand-alone class use of the Zend framework and only needs to be called.
I always can tell how useful a PHP framework is by trying to do what I learned first in PHP, building a HTML form. Not one post or tutorial I could find gave a straight forward answer a to how things worked. What was worse no one gave working code for using ZF2 as a library used by another simple PH script. Trying to get this done simply and without Zends MVC in place seemed to be unattainable. This meant that ZF2 was not something I would use at first thought. That has changed now that I have this code working. This is a complete stand-alone class use of the Zend framework and only needs to be called.
More About:
Content Connection Kit /
Zend Framework /
ZF2 Forms
Zend Framework and Google Maps API Example
When looking for a Senior PHP Developer position I get a lot of tests. Most of them I kindly refuse to do because they always involve array manipulation and have no real world context. They only show that the interviewer is not aware of what is real world. Sometimes the test is trying to solve problem that cannot be solved. I always ask who that works for them has solved the problem and can I meet them. Rather than babble on about the red flags that the answers to my question bring up I'll show my answer to a test. First because it was in the form of a spec. I like this. Secondly, because it involves using Google Maps API and object oriented PHP and Zend Framework I was happy to oblige. It was a useful real world test and harder than I expected because the Zend Framework v1 has horrible documentation and worse examples(none of them work). Zend Framework v2 has less. So after piecing together something that works I decided to share.
This is the code needed to connect to Google Maps API and display the results in JSON using ZF2 as a library.
/**
* @author Carl McDade
* @since 2013-05-09
* @version
*
* Use: go the the address bar and enter a place using the format
* http://berlinto.com/test.php?place=berlin
* http://berlinto.com/test.php?place=new jersey
* http://berlinto.com/test.php?place=1 hampton rd. England
*
* Berlin, Germany
*/
$path = __DIR__ .'/path/to/zend/library';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once('Zend/Loader/StandardAutoloader.php');
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
$loader->registerNamespaces(array('Zend'));
$loader->register();
use Zend\Http\Request;
use Zend\Http\Client;
class zftest{
protected $place;
function __construct()
{
$this->place = $_GET['place'];
}
function zend_test()
{
$uri = 'http://maps.google.com/maps/api/geocode/json';
$address = urlencode($this->place);
$sensor = 'false';
$request = new Request();
$request->setUri($uri);
$request->setMethod('GET');
$client = new Client($uri);
$client->setRequest($request);
$client->setParameterGet(array('sensor'=>$sensor,'address'=>$address));
$response = $client->dispatch($request);
if ($response->isSuccess())
{
header('Content-Type: text/html; charset=utf-8');
print 'Your Request for:' . print_r($address, 1) . '
';
print '' . print_r($response->getBody(), 1) . '
';
}
}
}
$maps = new zftest();
$maps->zend_test();
More About:
Google Maps /
PHP5 /
Zend Framework
Subscribe to:
Posts (Atom)