Subscribe to PHP Freaks RSS

Rapid Development of Zend Expressive Modules

syndicated from www.sitepoint.com on September 4, 2017

I have learned a few tricks when writing Zend Expressive modules that I want to share with you.

Please follow the previous post first to set up a proper working environment. I explained how to install and configure Zend Expressive with Doctrine, Gulp, and an abstract reflection factory - it'll take a total of 10 minutes.

In this tutorial, we'll build a simple read-only blog module (a page listing blog posts from a database) in minutes, demonstrating the kind of rapid development one is capable of with Zend Expressive.

Zend expressive logo

Module Setup

Run this command from your expressive app to get started:

./vendor/bin/expressive module:create Blog

This will generate some base code for a Blog module and will register your module automatically with your application. It will also register your module with the Composer autoloader.

Doctrine Entity and Database Table

Let's make our Blog entity and database tables. First, we need to let our application know that this module provides Doctrine entities.

Open src/Blog/src/ConfigProvider.php and add the following:

public function __invoke()
{
    return [
        'dependencies' => $this->getDependencies(),
        'doctrine'     => $this->getDoctrine(),
        'templates'    => $this->getTemplates(),
    ];
}

/** * @return array */ public function getDoctrine(): array { return [ 'driver' => [ 'orm_default' => [ 'drivers' => [ 'Blog\Entity' => 'blog_entity', ], ], 'blog_entity' => [ 'class' => \Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver::class, 'cache' => 'array', 'paths' => [ dirname(__DIR__) . '/config/doctrine' => 'Blog\Entity', ], ], ], ]; }

Create a blog post entity config at src/Blog/config/doctrine/BlogPost.orm.yml:

---
Blog\Entity\BlogPost:
  type: entity
  table: blog_post
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    title:
      type: string
      length: 255
    content:
      type: string
      length: 16777215

Then, run ./vendor/bin/doctrine orm:generate-entities src.

Continue reading %Rapid Development of Zend Expressive Modules%