I am implementing versioning in Laravel. Now my doubt is, where should I create the V1 subfolders within the various main folders? For example, is it okay for controllers (Controllers/Api/V1) but what about Models, Resources, Request, Services etc...?
1
-
4If you plan to evolve your service over time and might need to keep backwards compatibility to legacy clients there's no reason a v1 route would strictly use a v1 model or request or resource. You can evolve the rest of your code and ensure v1 routes still appear and function the same to the API consumers without necessarily using legacy code. Alternatively you can ditch the idea of writing a separate v2 of things and instead evolve your code as you would normally do and serve the updated Laravel app as a new app from a subpath using host configuration on your webserverapokryfos– apokryfos2023-01-28 20:35:55 +00:00Commented Jan 28, 2023 at 20:35
Add a comment
|
1 Answer
There is a very well maintained package specifically for what you are trying to do, called Laravel Modules: https://github.com/nWidart/laravel-modules
You can accomplish subversions of the application inside of it, and everything like "Models, Resource, Routes, etc.." is packaged seperately from one module to another.
Modules/
├── Blog/
├── Config/
├── Console/
├── Database/
├── factories/
├── Migrations/
├── Seeders/
├── Entities/
├── Http/
├── Controllers/
├── Middleware/
├── Requests/
├── Providers/
├── PostsServiceProvider.php
├── RouteServiceProvider.php
├── Resources/
├── assets/
├── lang/
├── views/
├── Routes/
├── api.php
├── web.php
├── Tests/
├── composer.json
├── module.json
├── package.json
├── webpack.mix.js
Check the documentation on how to use it: https://docs.laravelmodules.com/v9/creating-a-module
Also laravel-daily made a video on what it is here: https://www.youtube.com/watch?v=RVApkrYMcAg