Coder Social home page Coder Social logo

rester's Introduction

Rester

基于 Slim + Laravel Eloquent 的 RESTful API 框架。

由于 Laravel 的验证依赖了很多 Symfony 的组件,所以我简化了一个版本 (validation),并引用到这个项目中。

Usage

安装

请使用 composer 安装

git clone https://github.com/overtrue/rester
cd rester
composer install

虚拟机配置文件 vhost

路由:

app/routes.php

<?php

$app->get('/', function(){
    return json_encode(['hello' => 'world!']);
});

或者也可以使用控制器:

$app->get('/', 'HomeController:index'); //调用HomeController的index方法

更多路由的使用请阅读 Slim 官方文档:http://docs.slimframework.com/#Routing-Overview

控制器

<?php

/**
 * 演示控制器
 */
class HomeController extends Controller
{
    public function index() {
        return $this->json(['app' => 'Rester', 'message' => 'Hello world!']);
    }

    //...
}

控制器里有用的方法有:

  • $this->init()
    会在项目初始化完成后首先调用,可以用来做一些初始化或者每个方法都需要用到的动作,比如用户授权。

  • $this->json(mixed $data [, int $status = 200])
    输出 json 格式数据,第二个参数为状态码。

  • $this->jsonp(mixed $data [, string $callback = ''])
    输出 jsonp 数据,第二个参数为回调函数名,可选,为空默认从 $_GET 读取 callback, 如果最终获取不到 callback,输出等于 $this->json

  • $this->validate(array $input, array $rules [, boolean $return = false])
    数据验证,默认验证失败主动返回错误 json 输出并停止往下运行,当 $return = true 时不停止运行,返回验证失败消息。

  • $this->error(string $message, int $status [, array $errors = []]) 输出错误 json, $errors 为错误明细,通常为数组,可选。

更多数据验证规则请参阅:http://laravel.com/docs/4.2/validation

License

MIT

rester's People

Contributors

overtrue avatar

Watchers

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