GithubHelp home page GithubHelp logo

cxyac / php-rest-service Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nikhuber/php-rest-service

0.0 1.0 0.0 295 KB

Example web service implementing a RESTful API in DDD style using the API Platform framework

Dockerfile 11.31% Shell 3.07% PHP 84.10% HTML 1.52%

php-rest-service's Introduction

Example RESTful Web Service in PHP

This is a project you can use as a template to create and run a new service written in PHP.

Executing the sample service API

To execute the sample service, perform the following steps:

  1. Clone the project
  2. Install dependencies: ./initProject.sh ticketshop
  3. Adjust the .env files (in the project root an in ticketshop/) to your needs. Especially, in ticketshop/.envyou need to specify db as the hostname in the database URL string.
  4. Build the docker containers: docker-compose build
  5. Run the service: docker-compose up -d
  6. Create the schema: docker-compose exec php bin/console doctrine:schema:create

After these steps, you should be able to access the ticketshop API at http://localhost:8080/api

Implement your own API web service

If you want to create your own API web service, do the following:

  1. Clone the project
  2. Execute the init-script in the root folder and pass it the service name, e.g. ./initProject.sh yourname.
  3. Check and adjust your env-files in the project root and in the app folder (./yourname).
  4. Declare your entities and implement your business logic.

Custom Namespace

Maybe you want to use an individual namespace like Rx\Tickets for your classes instead of the default namespace App. If you decide to use your own namespace, its name should conform to the PSR-4 specification. In this example, we use the following settings:

Fully Qualified Class Name Namespace Prefix Base Directory Resulting File Path
\Rx\Tickets\Domain\Model\Ticket Rx\Tickets ./src/rx/tickets/ ./src/rx/tickets/Domain/Model/Ticket.php

To achieve this, perform the following steps. Note: the base path is your app directory, e.g. ticketshop/ not your project root!

  1. Add your new base directory to the autoloader configuration in the composer.json in the root directory:

     "autoload": {
        "psr-4": {
            "Rx\\Tickets\\": "src/rx/tickets/"
        }
     }
    
  2. Update the autoloader docker run --rm -v "$(pwd)":/app composer dump-autoload

  3. Adjust your ./config/services.yaml. Replace Appwith your namespace prefix, e.g. Rx\Ticket and point to the correct paths. See services.yaml for examples.

  4. Adjust the path(s) in ./config/packages/api_platform.yaml and ./config/routes/annotations.yaml

  5. Adjust paths and prefixes in ./config/packages/doctrine.yaml. See doctrine.yaml for examples.

Now, move all classes under /src to your new base directory, e.g. /src/rx/tickets. Don't forget to adjust all namespaces and usages or other references in your source code.

Tips

The following commands might help you to resolve issues:

docker-compose exec php bin/console cache:clear
docker-compose exec php bin/console debug:router

During implementation, I faced the following issues with the frameworks:

php-rest-service's People

Contributors

nikhuber avatar

Watchers

cxyac avatar

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.