Welche IDE ist die bessere?

Mittwoch, 13.06.2012 | Linux, PHP, Windows | Keine Kommentare | netbeans , php , java , ide , eclipse

Auf jedenfall Netbeans! Nach einigen Monaten des testens, konfigurierens und codens muss ich sagen das mir Netbeans besser gefällt als Eclipse. Als langjähriger Eclipse User habe ich mir Netbeans immer mal wieder angeschaut aber nicht wirklich produktiv genutzt. Erst als ich extrem von der Performance von Eclipse genervt war habe ich es mir genauer angesehen. Egal ob für PHP oder Java, für mich ist Netbeans besser geeignet. Es ist ressourcen schonender und bietet mehr funktionen.

Netbeans

Für die PHP Entwicklung ist es besonders gut geeignet weil auch Frameworks wie Zend Framework oder Symfony unterstützt werden. Außerdem wurde der HTML/CSS Editor hervorragend umgesetzt so das man im HTML Editor sogar auf Definitionen des Cascading Style Sheets zugreifen kann (Autovervollständigung).

Wenn Ihr euch bis jetzt nicht getraut habt Netbeans als IDE für die PHP Entwicklung zu benutzen oder auf der Suche nach einer neuen Entwicklungsumgebung seit dann kann ich es euch nur empfehlen!


Authentication with Zend Framework

Sonntag, 18.09.2011 | PHP | Keine Kommentare | php , zend framework

In this example I will show how to implement an authentication logic in Zend Framework with Zend_Auth. What we have is a module based MVC with two modules.

  • default - default module
  • restricted - restricted area (which needs authentication)

First of all we need a login action in our default module index controller for users to enter there credentails. In this example we authenticate against a Database Table where credentails are stored. There are more adapters available.

  • Database Table Authentication
  • Digest Authentication
  • HTTP Authentication Adapter
  • LDAP Authentication
  • Open ID Authentication
/* 
 * application/controllers/IndexController.php
 */
class IndexController extends Zend_Controller_Action
{
	public function loginAction()
    {
    	if($this->getRequest()->isPost()) {
    		$username = $this->_getParam('username', null);
    		$password = $this->_getParam('password', null);
    		
    		if(!is_null($username) && !is_null($password)) {
    			$dbAdapter = Zend_Registry::get('dbAdapter');
    			$authAdapter = new Zend_Auth_Adapter_DbTable(
				    $dbAdapter,
				    'users',     // Table name
				    'username',  // Idenitity column
				    'password'   // Password column
				);
				$authAdapter->setIdentity($username)->setCredential(md5($password));
				
				$auth = Zend_Auth::getInstance();
				$result = $auth->authenticate($authAdapter);
				
				if ($result->isValid()) {
					// Print the result row
					//print_r($authAdapter->getResultRowObject());
					$this->_forward('index', 'index', 'restricted');
					return;
				} else {
					echo 'invalid username/password';
				}
    		} else {
    			echo 'username/password is empty';
    		}
    	}
    }
}

Anyway if result is valid we can forward user to the restricted area (line 28).

Now we have to check if user is logged in. As we need to do this on every page it is a good idea to use a plugin. So let's create an authentication plugin.

/* 
 * library/My/Plugin/Auth.php
 */
class My_Plugin_Auth extends Zend_Controller_Plugin_Abstract 
{
	public function preDispatch()
	{
		$module = $this->getRequest()->getModuleName();
		if($module == 'restricted') {
			$auth = Zend_Auth::getInstance();
			if(!$auth->hasIdentity()) {
			 	$this->getRequest()->setModuleName('default');
			 	$this->getRequest()->setControllerName('index');
			 	$this->getRequest()->setActionName('login');
			}
		}
	}
}

If user has no identity and tries to view a page in the restricted area he will be redirected to the login page. To register this plugin we need to add the following line to our application.ini.

resources.frontController.plugins.one = "My_Plugin_Auth"

As Zend_Auth is implemented as singleton you can access it from everywhere. You can check if user has identity with following method:

Zend_Auth::getInstance()->hasIdentity()

How to use cURL through a Proxy Server in PHP

Donnerstag, 31.03.2011 | PHP | Keine Kommentare | php , proxy server , curl

Many companies allow access to the Internet through a Proxy Server, only. That makes it hard to developers to download files with standard features. One way is to use cURL. It's possible to setup cURL to use a Proxy Server.

In PHP it looks like this:

$res = curl_init();  
curl_setopt($res, CURLOPT_URL, 'http://www.someurl.com/');  
curl_setopt($res, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($res, CURLOPT_PROXY, '192.168.10.1:8080');  
curl_setopt($res, CURLOPT_PROXYUSERPWD,'user:pass');  
$string = curl_exec($res);  
curl_close($res); 

Zend Framework without mod_rewrite

Samstag, 05.02.2011 | PHP | 1 Kommentar | mod_rewrite , zend framework , php

For my hosting package, Apache mod_rewrite isn't enabled. As you can imagine it's not funny to use Zend Framework without it. With a little bit of coding it's possible to use the framework, anyway.

The first thing that we need is a own route. Rob Allen has created a route for URLs without mod_rewrite. This is a modified version of it

class My_Controller_Router_Route_RequestVars implements Zend_Controller_Router_Route_Interface
{
 protected $_current = array();

    /**
     * Instantiates route based on passed Zend_Config structure
     */
    public static function getInstance(Zend_Config $config)
    {
        return new self();
    }

    /**
     * Matches a user submitted path with a previously defined route.
     * Assigns and returns an array of defaults on a successful match.
     *
     * @param string Path used to match against this routing map
     * @return array|false An array of assigned values or a false on a mismatch
     */
    public function match($path)
    {
        $frontController = Zend_Controller_Front::getInstance();
        $request = $frontController->getRequest();
        /* @var $request Zend_Controller_Request_Http */
        
        $baseUrl = $request->getBaseUrl();
        if (strpos($baseUrl, 'index.php') !== false) {
            $url = str_replace('index.php', '', $baseUrl);
            $request->setBaseUrl($url);
        }
        
        $params = $request->getParams();
        
        if (array_key_exists('module', $params)
                || array_key_exists('controller', $params)
                || array_key_exists('action', $params)) {
            
            $module = $request->getParam('module', $frontController->getDefaultModule());
            $controller = $request->getParam('controller', $frontController->getDefaultControllerName());
            $action = $request->getParam('action', $frontController->getDefaultAction());

            $result = array('module' => $module, 
                'controller' => $controller, 
                'action' => $action, 
                );
            $this->_current = $result;
            return $result;
        }
        return false;
    }

    /**
     * Assembles a URL path defined by this route
     *
     * @param array An array of variable and value pairs used as parameters
     * @return string Route path with user submitted parameters
     */
    public function assemble($data = array(), $reset=false, $encode = false)
    {
        $frontController = Zend_Controller_Front::getInstance();
        
        if(!array_key_exists('module', $data) && !$reset 
            && array_key_exists('module', $this->_current)
            && $this->_current['module'] != $frontController->getDefaultModule()) {
            $data = array_merge(array('module'=>$this->_current['module']), $data);
        }
        if(!array_key_exists('controller', $data) && !$reset 
            && array_key_exists('controller', $this->_current) 
            && $this->_current['controller'] != $frontController->getDefaultControllerName()) {
            $data = array_merge(array('controller'=>$this->_current['controller']), $data);
        }
        if(!array_key_exists('action', $data) && !$reset 
            && array_key_exists('action', $this->_current)
            && $this->_current['action'] != $frontController->getDefaultAction()) {
            $data = array_merge(array('action'=>$this->_current['action']), $data);
        }
        
        $url = '';
        if(!empty($data)) {
            $url = '?' . http_build_query($data, '', '&');
        }

        return $url;
    }
}

To add this route, we need to tell the router about it in our index.php like this:

$bootstrap->bootstrap('frontController');
$front = $bootstrap->frontController;
$router = $front->getRouter();
$router->addRoute('requestVars', new My_Controller_Router_Route_RequestVars());

Second thing we need is a URL view helper that is used instead of Zend Frameworks URL view helper.

/**
 * URL view helper
 * 
 * @category   My
 * @package    My_View
 * @subpackage Helper
 * @author Burak Yueksel
 * @copyright Copyright (c) 2010 Burak Yueksel
 *
 */
class My_View_Helper_Url extends Zend_View_Helper_Abstract
{
	public function url(array $urlOptions = array(), $numericPrefix='', $argSeparator='&')
	{
		$request = Zend_Controller_Front::getInstance()->getRequest();
		
		if(!isset($urlOptions['module'])) $urlOptions['module'] = $request->getModuleName();
		if(!isset($urlOptions['controller'])) $urlOptions['controller'] = $request->getControllerName();
		if(!isset($urlOptions['action'])) $urlOptions['action'] = $request->getActionName();
		 
		$hyperLink = '/?module='.$urlOptions['module'].$argSeparator;
		$hyperLink .= 'controller='.$urlOptions['controller'].$argSeparator;
		$hyperLink .= 'action='.$urlOptions['action'].$argSeparator;
		
		unset(
			$urlOptions['module'], 
			$urlOptions['controller'], 
			$urlOptions['action']
		);
		
		if(sizeof($urlOptions)) {
			$hyperLink .= http_build_query($urlOptions, $numericPrefix, $argSeparator);	
		}
		return $hyperLink;
	}
}

This view helper will create URLs like this

/?module=index&controller=index&action=whatever&param1=hello&param2=world

This also works with Zends URL view helper but only if you reqeust the website with /?controller=index.

Now we need to add configuration for our own view helper to application.ini

resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/../library/My/View/Helper"

Thats it! Now we're able to build websites using Zend Framework without mod_rewrite.


SOAP Server mit dem Zend Framework

Mittwoch, 23.12.2009 | PHP | Keine Kommentare | php , wsdl , soap , webservices , zend framework

Um eine SOAP API zu programmieren benötigen wir erstmal eine Klasse, die alle Funktionen unserer neuen Schnittstelle bereitstellt.
<?php
// soap-server.php

$zendPath = '...';
set_include_path(get_include_path().PATH_SEPARATOR.$zendPath);

require 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();

class MeinWebservice {

    /**
     * Gibt den Wert mal 10 zurück
     *
     * @param int $inputParam
     * @return int
     */
    public function test1($inputParam) {
        return $inputParam * 10;
    }

    /**
     * Addiert die Werte
     *
     * @param int $inputParam1
     * @param int $inputParam2
     * @return int
     */
    public function test2($inputParam1, $inputParam2) {
        return $inputParam1 + $inputParam2;
    }

}
?>
Unsere Klasse "MeinWebservice" stellt also die zwei Funktionen test1 und test2 bereit. Damit diese Funktionen aufgerufen werden können, erstellen wir jetzt unseren SOAP Server.
<?php
// soap-server.php

$wsdl = 'http://localhost/soap/soap-server.php?wsdl=1';

if($_GET['wsdl'] == 1) {
  $autodiscover = new Zend_Soap_AutoDiscover();
  $autodiscover->setClass('MeinWebservice');
  $autodiscover->handle();
} else {
  $server = new Zend_Soap_Server($wsdl);
  $server->setClass('MeinWebservice');
  $server->handle();
}
?>
Mit $server->setClass() übergeben wir dem Zend_Soap_Server Objekt unsere Klasse. Um die WSDL automatisch aus unserer Klasse zu generieren verwenden wir Zend_Soap_AutoDiscover. Die WSDL wird aber nur erzeugt wenn der $_GET Parameter 'wsdl' auf 1 gesetzt wird. (Damit die WSDL richtig generiert wird muss der Docblock korrekt sein.) Ist 'wsdl' ungleich 1 übernimmt Zend_Soap_Server die verarbeitung. Um unseren Server zu testen erstellen wir uns einen Soap-Client und rufen unsere Funktionen auf.
<?php
// soap-client.php

$zendPath = '...';
set_include_path(get_include_path().PATH_SEPARATOR.$zendPath);

require 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();

$client = new Zend_Soap_Client("http://localhost/soap/soap-server.php?wsdl=1");

$result = $client->test1(10);
var_dump($result);

$result = $client->test2(50, 35);
var_dump($result);
?>
Weitere Informationen zu SOAP gibt es bei Wikipedia und im Zend Framework Refernce Guide.

Zend Framework in Action

Samstag, 17.01.2009 | PHP | 1 Kommentar | zend framework , php

Ich habe mich ja lange davor gesträubt das Zend Framework zu benutzen aber nachdem ich angefangen habe etwas damit zu Arbeiten muss ich sagen: Es macht wirklich Sinn! Man kann sehr schnell und sauber programmieren und alles hat seine Ordnung, vorallem wenn man das MVC System benutzt. Der Funktionsumfang des Frameworks ist rießig. Was mir besonders gefällt ist abgesehen vom Controller ist die Authentifizierung und der Paginator! Besonders erwähnenswert ist auch das Gdata Interface mit dem man auf alle wichtigen Google API's zugreifen kann. Das einzig negative ist die Performance! Das Framework scheint auch bei kleinen Web Applikationen etwas träge.


Netbeans 6.5 mit PHP unterstützung

Donnerstag, 27.11.2008 | PHP | Keine Kommentare | php , ide , netbeans

Nach PDT für Eclipse kommt jetzt auch Netbeans mit PHP unterstützung. Nachdem ich die Meldung gelesen habe musste ich Netbeans natürlich gleich ausprobieren... Und ich muss sagen das haben die Netbeans Entwickler garnicht schlecht gemacht! Klar, die Grundausstattung mit einem Editor für PHP inklusive Code-Vervollständigung, Syntaxhervorhebung und Debugging sowie einen Debugger für JavaScript ist drin. Besonders erwähnenswert ist das Datenbank Administrationswerkzeug mit dem Verbindungen zu allen gängigen Datenbanksystem aufgebaut werden können. So kann man sich den phpMyAdmin sparen! Schön ist noch das integrierte Subversion Tool. Alles in allem ist Netbeans 6.5 eine echte alternative zu Eclipse+PDT.

phpFeedCreator 2.0

Freitag, 18.07.2008 | PHP | Keine Kommentare | rss , php , phpfeedcreator

Heute habe ich die Version 2.0 des phpFeedCreators online gestellt. Der phpFeedCreator ist eine freie PHP Bibliothek zum erstellen von RSS 2.0 Feeds. Die neue Version wurde vollständig überarbeitet und von PHP 4 auf PHP 5 portiert. Die RSS 3 spezifikation und alle damit zusammenhängenden Funktionen wurden (mangels Weiterentwicklung) entfernt. Neu ist auch die Dokumentation die mit phpdoc generiert wurde. Wer das ganze mal ausprobieren möchte kann es bei Sourceforge herunterladen. Bei Fragen, Bugreports und Feature requests bitte einen Kommentar schreiben.

phpFeedCreator

Donnerstag, 22.03.2007 | PHP | Keine Kommentare | rss , php , phpfeedcreator

Der phpFeedCreator ist eine unter der GPL veröffentlichte PHP Library zum erstellen von RSS Feeds. Diese Library ist vollständig Objektorientiert Programmiert und leicht einsetzbar. Mit Hilfe von get und set Methoden lässt sich ein vollständiges RSS Feed erstellen. Das erstellte RSS Feed enthält nur die benötigten XML-Tags (platzsparend), alle nicht benutzen Features werden ignoriert. Die aktuelle RSS 3 spezifikation (First Draft) wird vollständig unterstützt. Die aktuelle Version ist 1.5.0! Sobald ich zeit habe werde ich noch ein paar Features hinzufügen also freut euch schonmal auf die 1.6 er Version. Der phpFeedCreator steht auf sourceforge.net zum Download bereit.