Friday, March 7, 2014

WebService ZF2 (RESTful)

RESTful Services made easy with ZF2.

Thera are many ways (technologies) to implement a web-services.

  • Representation State Transfer or REST , that it has been preferred for web services used in web applications. 
  • SOAP web service, also know as WS-* Stack, is also popular alternative. 
But REST has been more popular over web developers.

What is REST:

According to Abeysinghe "

REST is a software architecture style that can be followed while designing software systems. REST is an ideal design style to be followed for web services based software applications."
The key REST principles in brief: 
  • Provide every resource with a unique ID, for example, a URI Link resources with each other, establishing relationships among resources 
  • Use standard methods (HTTP, media types, XML) 
  • Resources can have multiple representations that reflect different application states 
  • The communication should be stateless using the HTTP
Rob Allen said that principal Constraints are:

  • Client/Server
  • Stateless
    •   No client context stored between requests. This means no sessions.
  • Cacheable
    • Non-idepoment methos should allow client to cache responses.
    • Clients should honor HTTP headers with respect to caching.
  • Layered system
    • Client should not care whether it is connected.
  • Uniform Interface
    • Identification of resources
    • Manipulation of resources through representations
    • Self-descriptive messages
    • Hypermedia as the engine of application state.

ZF2 Fundamentals:
   • PHP 5.3.3+ (and tested on 5.4, as well as upcoming
5.5)
• Modular & flexible (ModuleManager)
• Event-driven (EventManager)
• Leverage Inversion of Control (ServiceManager)
• Re-written MVC, Forms, I18n, Db, and more


Event Example   by  Rob Allen:
Simple example
 use Zend\EventManager\EventManager,
 Zend\EventManager\Event;
 $callback = function($event) {
 echo "An event has happened!\n";
 var_dump($event->getName());
 };
 $events = new EventManager();
 $events->attach('eventName', $callback);
 echo "\nRaise an event\n";
 $events->trigger('eventName');

LISTENER
Listeners
Just a function (Any callback)
 $callback = function($event) {
 echo "An event has happened!\n";
 var_dump($event->getName());
 var_dump($event->getTarget());
 var_dump($event->getParams());
 };
 $events = $someObject->getEventManager();
 $events->attach('eventName', $callback);

TARGET
Compose an EventManager within a class…
use Zend\EventManager\EventManager,
 Zend\EventManager\Event;
class MyTarget
{
 public $events;
 public function __construct()
 {
 $this->events = new EventManager();
 }
… and trigger actions within methods.
 public function doIt()
 {
 $event = new Event();
 $event->setTarget($this);
 $event->setParam('one', 1);
 $this->events->trigger('doIt.pre', $event);
 // do something here
 $this->events->trigger('doIt.post', $event);
 }Typical usage
$callback = function ($event) {
 echo "Responding to doIt.pre!\n";
 var_dump(get_class($event->getTarget()));
 var_dump($event->getName());
 var_dump($event->getParams());
};

$target= new MyTarget();
$target->events->attach('doIt.pre', $callback);
$target->doIt();


Thursday, March 6, 2014

Dont Forget .htaccess

How to read a file .htaccess over apache

If your RootDirectory is
/var/www directory.

use to active
$ sudo a2enmod rewrite
command and that should work, right?


Well, not really… Even though the issued command creates the symlink for the rewrite.load file at /etc/apache2/mods-enabled, you still have to manually edit your /etc/apache2/sites-enabled/000-default file and change the AllowOverride directive from None to All at the /var/www Directory section.

So the section that looks like:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None # change this line
    Order allow,deny
    allow from all
</Directory>
Should look like:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All # now it will read .htaccess
    Order allow,deny
    allow from all
</Directory>

Restart the service
service apache2 restart.
With AllowOverride directive to All , you are tell' the WebServer to read .htaccess  and load. Otherwise it wont read (AllowOverride None).


Monday, March 3, 2014

How to DB over ZF2

The model used to DB in ZF2 is PDO.
PDO helps to transform a database to Objects.

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server.

PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.


DB in ZF2 , we call MODEL
USING ZFTOOLS