0

How do I create the Routes for a RESTful API in Laravel 3?

I want to use GET, PUT, POST and DELETE in order to create an API.

I want all Routes to be prefixed with /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.

2 Answers 2

2

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. Here is a how to:

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

Also watch this video if you are new to APIs

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

What they suggest is passing parameters like so: 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. Also laravel 4 is said to release in May.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh OK. I know Laravel 4 is in Beta but if it releases in May time I might as well start using it. Thanks I will try following nettuts+ using Laravel 4
0

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.

So in routes.php, something like:

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

Though I'm new to Laravel too, and haven't looked into Events and Filters yet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.