Coder Social home page Coder Social logo

marcofsn / restful-codeigniter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from daemongh/restful-codeigniter

0.0 2.0 0.0 2.04 MB

RESTful style url Router Library for CodeIgniter

Home Page: http://liang.eu/web-dev/php/restful-style-url-in-codeigniter

PHP 47.30% ApacheConf 0.01% HTML 51.53% CSS 0.38% JavaScript 0.78%

restful-codeigniter's Introduction

RESTful-CodeIgniter

AUTHOR: ZHENJiNG LiANG ([email protected])

LINK: http://liang.eu/web-dev/php/restful-style-url-in-codeigniter

Introduction

RESTful-CodeIgniter is a RESful style url router library for CodeIgniter PHP Framework.

With this library's help, you can easily map a controller as a "Resource" (YES, like Rails), and then this controller can handles RESTful-style urls. See code below:

// GET /users
function index()
{
    echo __METHOD__ . "<br>\n" . $_SERVER['REQUEST_METHOD'] . " /users";
}

// GET /users/12345
function show($id)
{
    echo __METHOD__ . "<br>\n" . $_SERVER['REQUEST_METHOD'] . " /users/{$id}";
}

// GET /users/new
// I have to use new_form as method name because "new" is a keyword in php
function new_form()
{
    echo __METHOD__ . "<br>\n" . $_SERVER['REQUEST_METHOD'] . " /users/new";
}

// GET /users/12345/edit
function edit($id)
{
    echo __METHOD__ . "<br>\n" . $_SERVER['REQUEST_METHOD'] . " /users/{$id}/edit";
}

// POST /users
function create()
{
    echo __METHOD__ . "<br>\n" . $_SERVER['REQUEST_METHOD'] . " /users";
}

// PUT /users/12345
function update($id)
{
    echo __METHOD__ . "<br>\n" . $_SERVER['REQUEST_METHOD'] . " /users/{$id}";
}

// DELETE /users/12345
function delete($id)
{
    echo __METHOD__ . "<br>\n" . $_SERVER['REQUEST_METHOD'] . " /users/{$id}";
}    
...

Sounds great, How can I install && use it?

This library is designed to be easily install and easily configure.

Installation:

All you need to do is drop MY_Router.php into you appliation's libraries directory("system/application/libraries" by default) and everything is done!

Configuration:

Open application/routers.php, add this line at the bottom, and you are done!

map_resources('users');

Now you can test these urls by curl:

$ curl http://localhost/restful-codeigniter/users
$ curl http://localhost/restful-codeigniter/users/new
$ curl http://localhost/restful-codeigniter/users/9999
$ curl http://localhost/restful-codeigniter/users/9999/edit
$ curl -X PUT http://localhost/restful-codeigniter/users/9999
$ curl -X POST http://localhost/restful-codeigniter/users/9999
$ curl -X DELETE http://localhost/restful-codeigniter/users/9999

I need more...

You can register additional custom handlers in routes.php:

//syntax:
// map_resources($request_method, $pattern, $replace)

// examples:

// custom action handles GET request
map_resources('GET', 'users/(:id)/custom_action', 'users/custom_action/$1'); 

// some other custom action mappings...
// custom action handles PUT request
map_resources('PUT', 'users/(:id)/custom_action', 'sync_sessions/custom_action/$1');

// custom action handles all requests (GET, POST, PUT, DELETE)
map_resources('users/(:id)/custom_action', 'sync_sessions/custom_action/$1');

LICENSE

This library is released under MIT license.

restful-codeigniter's People

Watchers

 avatar  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.