简体   繁体   English

Laravel 3 - RESTful API

[英]Laravel 3 - RESTful API

How do I create the Routes for a RESTful API in Laravel 3? 如何在Laravel 3中为RESTful API创建路由?

I want to use GET, PUT, POST and DELETE in order to create an API. 我想使用GET,PUT,POST和DELETE来创建API。

I want all Routes to be prefixed with /v1/ 我希望所有路由都以/v1/为前缀

So, I can do this: 所以,我可以这样做:

http://api.example.com/v1/controller/method/parameter and just create the relevant controllers to check for Auth and perform actions. http://api.example.com/v1/controller/method/parameter并创建相关的控制器以检查Auth并执行操作。

I would suggest you using laravel 4 instead of laravel 3. Laravel 4 is really good at creating RESTful APIs and you can get started pretty fast. 我建议你使用laravel 4而不是laravel 3. Laravel 4非常擅长创建RESTful API,你可以很快开始使用。 Here is a how to: 这是一个如何:

http://net.tutsplus.com/tutorials/php/laravel-4-a-start-at-a-restful-api/ http://net.tutsplus.com/tutorials/php/laravel-4-a-start-at-a-restful-api/

Also watch this video if you are new to APIs 如果您不熟悉API,请观看此视频

https://blog.apigee.com/detail/restful_api_design https://blog.apigee.com/detail/restful_api_design

What they suggest is passing parameters like so: api.test.com/v1/dogs?state=running 他们建议传递如下参数:api.test.com/v1/dogs?state=running

I recently started of with developing a restful API service myself using laravel 4 and it is going pretty well so far. 我最近开始使用laravel 4开发一个安静的API服务,到目前为止它还很顺利。 Also laravel 4 is said to release in May. 据说laravel 4也将于5月发布。

With already existing routes, I would presume that you would need to add a filter with 'before' then prefix the "url" passing it back to the original route. 对于已经存在的路由,我认为您需要添加一个带有'before'的过滤器,然后将“url”作为前缀,将其传递回原始路由。

So in routes.php, something like: 所以在routes.php中,类似于:

Route::get('login', 'user@login', array('before' => 'guest'));
Route::post('login', 'user@login', array('before' => 'guest'));
Route::get('logout', 'user@logout', array('before' => 'auth'));

Route::filter('before', function()
{
    // Do stuff before every request to your application...
        $url = "test";
        $controller = "user@test";
        $filter = array('before' => 'guest');
        return Route::get('/v1/' . $url, $controller, $filter);
});

But I'm not sure how you can fill in the $url, $controller, $filter with the incoming request (maybe Request:: has something). 但是我不知道如何用传入的请求填写$ url,$ controller,$ filter(也许Request ::有东西)。

Though I'm new to Laravel too, and haven't looked into Events and Filters yet. 虽然我也是Laravel的新手,但还没有看过事件和过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM