• This repository has been archived on 21/Sep/2019
  • Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 13 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Extension for the popular Zend Framework, allows to create RESTful Controllers and build API endpoints with ease.

RESTful Applications with Zend Framework

This extension for Zend Framework, allows to create RESTful Controllers with ease. please keep in mind that these instructions are general and you should probably customize the code to fit your needs.

for a working example please refer to github.com/ahmadnassri/restful-zend-framework-example

Assumptions

  • you are building a mixed application (regular ZF Controllers + RESTful controllers)
  • your controllers can be a a mix of regular controllers and RESTful controllers or a hybrid!

I recommend creating a separate module for the RESTful controllers, its less complicated to manage this way, and you don't have to worry about advanced REST routing ... However, you can have pie and eat it too! It's possible to have any single Controller act as both a REST controller and a typical Zend MVC controller (HTML output).

In this particular example, I'm using a separate module "Api" that is purely used for REST API calls.

Steps

  1. Copy the REST directory into your library.
  2. modify application.ini.
  3. modify application/Bootstrap.php.
  4. modify your RESTful module Bootstrap ex: application/modules/api/Bootstrap.php.
  5. create Controllers as usual, just make sure they extends REST_Controller.
  6. check https://github.com/ahmadnassri/restful-zend-framework-example for examples.
  7. reccomended: use the Api_ErrorController provided in the example above, modify to your needs.

application.ini:

add the following:

autoloaderNamespaces[] = "REST_"

rest.default = "xml"
rest.formats[] = "json"
rest.formats[] = "xml"

the above achieves a couple of things:

  1. Autoloads the REST library
  2. sets the default respond format when all content type negotiation fails (in the above example: xml)
  3. determines the list of content types you want to support in your API, built in types include: html, xml, php, json

application/Bootstrap.php

add the following:

public function _initREST()
{
    $frontController = Zend_Controller_Front::getInstance();

    // set custom request object
    $frontController->setRequest(new REST_Request);
    $frontController->setResponse(new REST_Response);

    // add the REST route for the API module only
    $restRoute = new Zend_Rest_Route($frontController, array(), array('api'));
    $frontController->getRouter()->addRoute('rest', $restRoute);
}

In the above example, we are only enabling RESTful responses on a particular route, which is the Api module, look up the Zend_Rest_Route docs for further configuration options.

application/modules/api/Bootstrap.php

Note: depending on your setup, you may want to setup some advanced rules to enable the REST Plugin and the Action Helpers only when needed. I use a modified Bootstraping method called "Active Bootstrap" (google it) to only run the bootstrap _init methods per active module, which saves me a lot of headaches.

public function _initREST()
{
    $frontController = Zend_Controller_Front::getInstance();

    // register the RestHandler plugin
    $frontController->registerPlugin(new REST_Controller_Plugin_RestHandler($frontController));

    // add REST contextSwitch helper
    $contextSwitch = new REST_Controller_Action_Helper_ContextSwitch();
    Zend_Controller_Action_HelperBroker::addHelper($contextSwitch);

    // add restContexts helper
    $restContexts = new REST_Controller_Action_Helper_RestContexts();
    Zend_Controller_Action_HelperBroker::addHelper($restContexts);
}

Module Specific ErrorController issue

It seems there is an inherit issue with Zend Framework's modules & calling the ErrorController, basically ZF calls the default module's error controller for all modules. This can be a problem of course if one of your modules is an API, you'll end up with HTML in the REST ErrorController output.

to fix this is beyond the scope of the REST library, so its only included in the README file:

in your application.ini

resources.frontController.plugins.ErrorHandler.class = "Zend_Controller_Plugin_ErrorHandler"
resources.frontController.plugins.ErrorHandler.options.module = "default"
resources.frontController.plugins.ErrorHandler.options.controller = "error"
resources.frontController.plugins.ErrorHandler.options.action = "error"

then create a plugin to change the "module" scope, you can name this whatever you want, I went with App_Controller_Plugin_Errors:

class App_Controller_Plugin_Errors extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $frontController = Zend_Controller_Front::getInstance();

        $error = $frontController->getPlugin('Zend_Controller_Plugin_ErrorHandler');

        $error->setErrorHandlerModule($request->getModuleName());
    }
}

Bugs and feature requests

Have a bug or a feature request? Please first read the issue guidelines and search for existing and closed issues. If your problem or idea is not addressed yet, please open a new issue.

Contributing

Please read through our contributing guidelines. Included are directions for opening issues, coding standards, and notes on development.

More over, if your pull request contains JavaScript patches or features, you must include relevant unit tests.

Editor preferences are available in the editor config for easy use in common text editors. Read more and download plugins at http://editorconfig.org.

Support

Donations are welcome to help support the continuous development of this project.

Gratipay PayPal Flattr Bitcoin

License

MIT © Ahmad Nassri

More Repositories

1

action-dependabot-auto-merge

Automatically merge Dependabot PRs when version comparison is within range
JavaScript
341
star
2

app-restconsole

HTTP Client and Request Visualizer and Constructor tool, helps developers build, debug and test RESTful APIs
JavaScript
243
star
3

awesome-startup-resources

A curated list of useful resources for building a new startup business
141
star
4

docker-vscode-server

a Docker image for VS Code Server
Makefile
89
star
5

node-har-validator

Extremely fast HTTP Archive (HAR) validator using JSON Schema
JavaScript
61
star
6

css-diagonal-separators

Pure CSS Diagonal Separators
HTML
58
star
7

action-workflow-queue

if the same workflow is already running from a previous commit, wait for it to finish
JavaScript
57
star
8

node-har

HTTP Archive (HAR) Dynamic Object
JavaScript
54
star
9

node-glob-promise

Promise version of glob
JavaScript
53
star
10

awesome-coworking

Directory of Awesome Co-Working spaces around the globe
52
star
11

action-workflow-run-wait

wait for all `workflow_run` required workflows to be successful
JavaScript
37
star
12

action-terraform-report

Updates Pull Requests with visual diff of Terraform Plan changes
JavaScript
35
star
13

har-spec

The HTTP Archive Spec
31
star
14

mkdirp-promise

Promise version of mkdirp
JavaScript
30
star
15

docs-engineering-matrix

Engineering Team Competency & Skill Matrix
29
star
16

restful-zend-framework-example

Example RESTful Application using Zend Framework 1.0
PHP
28
star
17

har-resources

A community curated list of resources, tools, projects and applications that support HTTP Archive (HAR).
HTML
28
star
18

node-pretty-exceptions

Pretty and more helpful uncaught exceptions, automatically
JavaScript
26
star
19

rest-codes

JavaScript
23
star
20

node-api-problem

HTTP Problem Utility
JavaScript
22
star
21

node-metalsmith-pug

Metalsmith plugin to convert jade files
JavaScript
21
star
22

awesome-accelerators

A curated list of startup accelerators around the globe
20
star
23

har-schema

schema package for HTTPArchive (HAR)
JavaScript
19
star
24

node-metalsmith-paths

Metalsmith plugin that adds file path values to metadata
JavaScript
19
star
25

node-metalsmith-imagemin

Metalsmith plugin to minify images
JavaScript
18
star
26

node-nightwatch-accessibility

Nightwatch.js utility assertion for accessibility testing with aXe
JavaScript
18
star
27

benchmark-node-clone

Node Clone Benchmarks
JavaScript
17
star
28

echint

Quick validation of files against EditorConfig
JavaScript
16
star
29

node-april-fools

A time bomb that will throw a randomly generated `Error` on April 1st
Makefile
15
star
30

node-metalsmith-request

Metalsmith plugin to grab content from the web and expose the results to metadata
JavaScript
15
star
31

action-semantic-release

Semantic Release with all the presets
JavaScript
13
star
32

google-maps-polygon-rotate

Programmatically rotate a google.maps.Polygon around an angle or its center using Google Maps V3
JavaScript
13
star
33

node-oas-request

OAS 3.x dynamic request client
JavaScript
12
star
34

benchmark-node-json-parse

What is the fastest method to parse JSON files?
JavaScript
10
star
35

action-template-repository-sync

Keep projects in sync with the template repository they came from
JavaScript
10
star
36

node-autoenv

Automatically loads environment variables from named .env files
Makefile
9
star
37

awesome-incubators

A curated list of startup incubators around the globe
9
star
38

docker-node-puppeteer

node & puppeteer in one container
Dockerfile
9
star
39

action-google-cloud-sql-proxy

Shell
8
star
40

template-js-lib

a template repo for a node project
JavaScript
8
star
41

pkg-config

parse the closest package.json and get package specific configurations
JavaScript
8
star
42

gaza-everywhere

A Google Maps app to help visualize Gaza's size in relative to locations around the world
JavaScript
8
star
43

node-oas-fastify

OAS 3.0 to Fastify routes automation
JavaScript
8
star
44

docker-github-pages

jekyll & github-pages in one container
Makefile
7
star
45

node-spawn-promise

Child Process Spawn as a Promise, with simple in/out piping
JavaScript
7
star
46

forwarded-http

Resolve RFC 7239 (Forwarded HTTP Extension), with fallback to all legacy & special Forward headers
JavaScript
7
star
47

action-commit-lint

commitlint your PRs with a default configuration auto applied
JavaScript
7
star
48

template-node-lib

a template repo for a node project
Makefile
6
star
49

har-cli

HAR tools in the command line
JavaScript
6
star
50

node-serve-reload-replace

simple http server with built-in live reload, server-sent events, server side includes, and more!
JavaScript
6
star
51

node-template-literals-engine

a very simple template engine for template literals
JavaScript
6
star
52

oh-my-log

Beautiful console logs for your console application
JavaScript
6
star
53

node-updated

check for updated package.json dependencies
JavaScript
5
star
54

template-template

A template for GitHub Template Repositories!
Makefile
5
star
55

parakeet

Echo server for TCP/UDP/HTTP
JavaScript
5
star
56

template-docker

a template for Docker projects
Makefile
4
star
57

node-fs-writefile-promise

Promise version of fs.writefile
JavaScript
4
star
58

action-github-registry-npm-proxy

sets up an .npmrc file that points to GPR as a proxy
Makefile
4
star
59

furmat

super powered printf & util.format equivalent string formatting, with locals & chainable modifiers
JavaScript
4
star
60

chrome-link-preview

Right click on a link to quickly preview its content in a nice summary card, without leaving the page
JavaScript
4
star
61

node-marked-promise

Promise version of marked
Makefile
3
star
62

logress

log your progress, with logress!
JavaScript
3
star
63

node-smart-promise

a Promise extension that provides filtered catch handler
JavaScript
3
star
64

node-error

Extendable Error Class for use with Node >= 4.x
JavaScript
3
star
65

action-changed-files

detect changed files in a Commit or PR
Shell
3
star
66

actions

Makefile
3
star
67

node-winston-tcp

TCP transport for Winston
JavaScript
3
star
68

node-stringify-clone

Wrapper for fast object cloning using `JSON.parse` & `JSON.stringify`
Makefile
3
star
69

i-jest-you-not

Who needs Jest anyways?
JavaScript
2
star
70

node-debug

Debugging utility using environment regex, matches node core's debugging technique
JavaScript
2
star
71

node-uncaught-extender

Extends uncaughtException and unhandledRejection with custom listeners
JavaScript
2
star
72

docker-mermaid-cli

docker image for mermaid-cli with puppeteer
Makefile
2
star
73

action-metadata

get all the repo and event metadata for use in Actions
Shell
2
star
74

template-action-composite

a template repo for a composite github actions
Makefile
2
star
75

docker-gollum

docker image for gollum wiki
Makefile
2
star
76

action-npm-global-path

configure GitHub Actions to work with npm --global
Makefile
1
star
77

node-install-group

Dependency grouping for npm-install
JavaScript
1
star
78

docker-rapidoc-server

RapiDoc server with live reload
HTML
1
star
79

simple-file-cache

a simple and easy to use file-based cache
JavaScript
1
star
80

template-action-docker

a template repo for docker-based github actions
Makefile
1
star
81

template-action-node

a template repo for node.js github actions
Makefile
1
star
82

node-oas-schemas

OAS Schemas
JavaScript
1
star
83

template-terraform

A template for Terraform Template Repositories!
Makefile
1
star
84

docker-unifi-api-browser

A docker image for UniFi-API-Browser
PHP
1
star
85

node-cloudevents-schemas

CloudEvents Schema
Makefile
1
star
86

action-dotenv

assign .env files values into $GITHUB_ENV
Makefile
1
star
87

action-slack-workflow-notifications

Fully detailed GitHub Actions workflow notifications in Slack
JavaScript
1
star
88

logo-builder

a simple logo spec builder
JavaScript
1
star
89

node-test

improved native test runner for node.js
Makefile
1
star