How to organize different versioned REST API controllers in Laravel 4?

Asked
Active3 hr before
Viewed126 times

6 Answers

different
90%

I know there's a way to create versioned URLs for REST APIs with routes, but what's the best way to organize controllers and controller files? I want to be able to create new versions of APIs, and still keep the old ones running for at least some time.,I'm not positive this is the best solution. However, it has allowed versioning of the controllers in a way that they do not interfere with each other. You could probably do something verify similar with the models if needed., By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

I ended up using namespaces and directories under app/controllers:

/app /
controllers
   /
   Api /
   v1 /
   UserController.php /
   v2 /
   UserController.php
load more v
88%

I know there's a way to create versioned URLs for REST APIs with routes, but what's the best way to organize controllers and controller files? I want to be able to create new versions of APIs, and still keep the old ones running for at least some time.,I'm not positive this is the best solution. However, it has allowed versioning of the controllers in a way that they do not interfere with each other. You could probably do something verify similar with the models if needed.,I ended up using namespaces and directories under app/controllers:,And in UserController.php files I set the namespace accordingly:

I ended up using namespaces and directories under app/controllers:

/app /
controllers
   /
   Api /
   v1 /
   UserController.php /
   v2 /
   UserController.php
load more v
72%

I know there's a way to create versioned URLs for REST APIs with routes, but what's the best way to organize controllers and controller files? I want to be able to create new versions of APIs, and still keep the old ones running for at least some time.,How to organize different versioned REST API controllers in Laravel 4? ,I'm not positive this is the best solution. However, it has allowed versioning of the controllers in a way that they do not interfere with each other. You could probably do something verify similar with the models if needed.,I ended up using namespaces and directories under app/controllers:

I ended up using namespaces and directories under app/controllers:

/app /
controllers
   /
   Api /
   v1 /
   UserController.php /
   v2 /
   UserController.php
load more v
65%

1.2. / app3. / controllers4. / Api5. / v16. / UserController.php7. / v28. / UserController.php9 .10.namespace Api\ v1;
11.12.namespace Api\ v2;
13.14.Route::group(['prefix' => 'api/v1'], function() {
   15. Route::get('user', 'Api\v1\[email protected]');
   16. Route::get('user/{id}', 'Api\v1\[email protected]');
   17.
});
18.19.Route::group(['prefix' => 'api/v2'], function() {
   20. Route::get('user', 'Api\v2\[email protected]');
   21. Route::get('user/{id}', 'Api\v2\[email protected]');
   22.
});
23.
75%

It seems most people are following this approach: How to organize different versioned REST API controllers in Laravel 4?,My questions is: What if I am building a minor upgrade of the API, say v1.1, how do I organize my folder structure? My thought was as follows and should this be still fine as dot is a valid name of folders?,The idea itself is solid. But planning an API is hard work. I'd recommend you write up a good list of all your expected endpoints and draft a couple of API versions then select the best one.,POST is the all-purpose, swiss-army knife of the protocol and can be used to literally manage resources to your likings. As the semantics are defined by the developer you can use it to create, update or delete multiple resources at once.

Folders structures:

/app /
controllers
   /
   Api /
   v1 /
   UserController.php /
   v2 /
   UserController.php

And in UserController.php files I set the namespace accordingly:

namespace Apiv1;

or

namespace Apiv2;

and in routes:

Route::group(['prefix' => 'api/v1'], function() {
   Route::get('user', 'Apiv1UserController@index');
   Route::get('user/{id}', 'Apiv1UserController@show');
});

Route::group(['prefix' => 'api/v2'], function() {
   Route::get('user', 'Apiv2UserController@index');
   Route::get('user/{id}', 'Apiv2UserController@show');
});

My questions is:
What if I am building a minor upgrade of the API, say v1.1, how do I organize my folder structure?
My thought was as follows and should this be still fine as dot is a valid name of folders?

/app /
controllers
   /
   Api /
   v1 /
   UserController.php /
   v1 .1 /
   UserController.php /
   v1 .2 /
   UserController.php /
   v2 /
   UserController.php

Also, how should I write the namespace? There is no namespace like this

namespace Apiv1 .1;
load more v
40%

RESTful Resource Controllers,If you choose to nest or organize your controller using PHP namespaces, simply use the fully qualified class name when defining the route:,Here is an example of a basic controller class:,By default, all resource controller actions have a route name; however, you can override these names by passing a names array with your options:

Here is an example of a basic controller class:

class UserController extends BaseController {

   /**
    * Show the profile for the given user.
    */
   public
   function showProfile($id) {
      $user = User::find($id);

      return View::make('user.profile', array('user' => $user));
   }

}
load more v

Other "different-undefined" queries related to "How to organize different versioned REST API controllers in Laravel 4?"