GithubHelp home page GithubHelp logo

bsormagec / slim-php-mongo-rest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattmcfarland/slim-php-mongo-rest

0.0 2.0 0.0 264 KB

MongoDB REST server using Slim PHP

ApacheConf 0.09% PHP 99.91%

slim-php-mongo-rest's Introduction

Slim PHP Mongo REST server

MongoDB REST server using Slim PHP.

I've just thrown this up quickly, there might be an error or two. But I am hoping to polish it a bit and add a few features to bring it more in line with the Node.js one I have used before. Probably a good starting point for people looking to do more advanced things, or just of interest anyone looking into Slim or Mongo with PHP. And if someone uses it and I get a pull request, all the better!

Requirements

PHP, with MongoDB driver http://php.net/manual/en/mongo.installation.php

A web server such as nginx, lighttpd or Apache httpd

Notes

Update will only update fields present in the request, leaving any existing fields in tact. I will add the faster 'save' update (which is effectively a delete/insert for the same key) in future.

Usage

Configure MONGO_HOST at the top of index.php

Here's some jQuery -

Fetch collection

Get all in collection (respecting limit set in mongo-list.php)

$.getJSON(
  '/database/collection',
  function(data) {
    console.log(data);
  }
);

Get blogs with phone in the title

$.ajax({
    url: '/database/collection',
    type: 'GET',
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify({
      filter: {
        type: 'blogs'
      },
      wildcard: {
        title: 'phone'
      }
    }),
    success: function(data) {
      console.log(data);
    },
    error: function (data) {
      console.log(data);
    }
});

And in Backbone

var collection = Backbone.Collection.extend({
  url: '/database/collection',
  // drill down to the actual results
  parse: function(response) {
    return response.results;
  }
});

collection.fetch({
  data: {
    filter: {
      type: 'blogs'
    }
    // etc.
  }
});

Save document

$.ajax({
  url: '/database/collection',
  type: 'POST',
  contentType: 'application/json',
  dataType: 'json',
  data: JSON.stringify({
    title: 'My Object',
    body: 'text'
    // etc.
  }),
  success: function(data) {
    console.log(data);
  },
  error: function (data) {
    console.log(data);
  }
});

Update document

$.ajax({
  url: '/database/collection/id',
  type: 'PUT',
  contentType: 'application/json',
  dataType: 'json',
  data: JSON.stringify({
    title: 'My Object',
    body: 'text'
    // etc.
  }),
  success: function(data) {
    console.log(data);
  },
  error: function (data) {
    console.log(data);
  }
});

Todo

  • Save/Update/Delete multiple
  • Alternate update using save()
  • Check it works... :-D

slim-php-mongo-rest's People

Contributors

joevallender avatar

Watchers

James Cloos avatar Burak Sormageç 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.