Giter Site home page Giter Site logo

beachcasts / doctrine-expressive-example Goto Github PK

View Code? Open in Web Editor NEW
38.0 3.0 25.0 123 KB

Example PHP REST API application built with Expressive using Doctrine ORM, complete with associations, pagination, and DB connectivity inside Docker containers. Used in #Beachcasts videos on YouTube.

Home Page: https://www.youtube.com/playlist?list=PL6_nF0awZMoMol4RPLf99WIZuoJ3l87oG

License: MIT License

PHP 93.10% Dockerfile 1.55% Shell 0.16% TSQL 4.96% Gherkin 0.23%
zend-expressive doctrine-orm docker-compose rest-api doctrine2 docker-container beachcasts

doctrine-expressive-example's Introduction

CircleCI

doctrine-expressive-example

Example PHP REST API application spread across 2 Docker containers. (1=Apache, 2=MySQL) I used Zend Expressive with Doctrine ORM in an effort to help document usage of both in a typical scenario. The application leveraged the zend-expressive-skeleton as a starting point, and was installed set up for Modules using FastRoute, zend-servicemanager, and no template engine (since it is a REST API responding with Json).

The Doctrine Entities also include example usage of Associations between Entities (see the OneToMany association in Bank using ArrayCollection to ManyToOne association in Branch) as well as parent-child relationships within the same Entity (See the Bank associations - ManyToOne $parent and OneToMany $children using ArrayCollection).

Knowledge shared through example

  • Zend Expressive usage
    • JsonResponse usage
    • Middleware usage
    • FastRoute usage
    • UrlHelper usage for hypermedia link creation
  • container-interop-doctrine usage
  • ramsey/uuid and ramsey/uuid-doctrine usages for primary keys in Announcements module, where routes also had to accommodate the UUID pattern of URLs - see the RoutesDelegator.php.
  • Doctrine ORM usage
    • OneToMany Bidirectional and self-referencing association example (See Bank and Branch Entities)
    • ManyToOne unidirectional association example (See Bank and Branch Entities)
    • ArrayCollection example (See Bank and Branch Entities)
    • EntityManager examples
    • RepositoryManager examples
    • Pagination usage (see Read Handlers)
    • Associations represented in data output (see View Handlers)
    • CLI config
  • PHP v7.2 feature usage

Installation

  • Step 1 - Clone this repo to desired location
  • Step 2 - Change directory to newly cloned repo via CLI cd /new/cloned/location
  • Step 3 - Run docker-compose docker-compose up -d
  • Step 4 - copy/rename the following config files
    • /config/autoload/development.local.php.dist >>> development.local.php
    • /config/autoload/doctrine.local.php.dist >>> doctrine.local.php
    • /config/autoload/local.php.dist >>> local.php
    • /config/development.config.php.dist >>> development.config.php
  • Step 5 (optional) - Use docker-compose to clean up the example application docker-compose down

Usage

At this point the REST API should work. (The following REST endpoints return a listing from the DB.)

Each of these can be called as an HTTP GET, or an HTTP POST with Json fields payload. (See example body content in docblocks of Create and Update Handlers. Sample Json data for requests can be found in /data.)

In addition to these endpoints the hypermedia in each response provides information to additional endpoints.

CLI Tools

Also, the Zend Expressive, Doctrine DBAL, and Doctrine CLI commands are available by gaining the terminal from Docker.

  • docker ps will provide the container ids to use in the following command.
  • docker exec -i -t {container-id} bash

Replace {container-id} with the Docker container ID from the docker ps for the web container.

Then from the root prompt the CLI tools are available. Example commands are:

  • php vendor/bin/doctrine list
  • php vendor/bin/doctrine-dbal list
  • php vendor/bin/expressive
  • or php vendor/doctrine/bin/doctrine list

Constructive advice welcome

This code is an example, but any advice to help make this a more awesome teaching tool is welcome. Please submit a ticket with details, or better yet, submit a pull request. Thank you.

Behavioral API testing

coming soon

doctrine-expressive-example's People

Contributors

adamculp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

doctrine-expressive-example's Issues

E: Package 'mysql-client' has no installation candidate

Hello
thank you for your good tutorial,
I just got an error when I run docker-compose up -d
the error is:
Building web
Step 1/3 : FROM php:7.2-apache
---> 2aefc00193b5
Step 2/3 : RUN apt-get update && apt-get install -y vim git zlib1g-dev mysql-client libzip-dev && docker-php-ext-install zip mysqli pdo_mysql && pecl install xdebug && docker-php-ext-enable xdebug && echo 'xdebug.remote_enable=on' >> /usr/local/etc/php/conf.d/xdebug.ini && echo 'xdebug.remote_host=host.docker.internal' >> /usr/local/etc/php/conf.d/xdebug.ini && echo 'xdebug.remote_port=9000' >> /usr/local/etc/php/conf.d/xdebug.ini && a2enmod rewrite && sed -i 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/000-default.conf && mv /var/www/html /var/www/public &&
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && echo "AllowEncodedSlashes On" >> /etc/apache2/apache2.conf
---> Running in 285499a20460
Get:1 http://cdn-fastly.deb.debian.org/debian buster InRelease [122 kB]
Get:3 http://security-cdn.debian.org/debian-security buster/updates InRelease [39.1 kB]
Get:2 http://cdn-fastly.deb.debian.org/debian buster-updates InRelease [49.3 kB]
Get:4 http://security-cdn.debian.org/debian-security buster/updates/main amd64 Packages [85.0 kB]
Get:5 http://cdn-fastly.deb.debian.org/debian buster/main amd64 Packages [7899 kB]
Get:6 http://cdn-fastly.deb.debian.org/debian buster-updates/main amd64 Packages [884 B]
Fetched 8194 kB in 4s (2192 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
Package mysql-client is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'mysql-client' has no installation candidate
ERROR: Service 'web' failed to build: The command '/bin/sh -c apt-get update && apt-get install -y vim git zlib1g-dev mysql-client libzip-dev && docker-php-ext-install zip mysqli pdo_mysql && pecl install xdebug && docker-php-ext-enable xdebug && echo 'xdebug.remote_enable=on' >> /usr/local/etc/php/conf.d/xdebug.ini && echo 'xdebug.remote_host=host.docker.internal' >> /usr/local/etc/php/conf.d/xdebug.ini && echo 'xdebug.remote_port=9000' >> /usr/local/etc/php/conf.d/xdebug.ini && a2enmod rewrite && sed -i 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/000-default.conf && mv /var/www/html /var/www/public && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && echo "AllowEncodedSlashes On" >> /etc/apache2/apache2.conf' returned a non-zero code: 100

hope you can help me to fix it :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.