why I should duplicate the code in the second version api, I was working on a graduation project with the same idea and I not duplicated the code I still call the first api functions in case I not have a plan for adding a new actions, but in case I have a new actions I use your approach.
We store preferred language in local storage and map it on every request to the Accept-Language header. We then parse than Header in a middleware and use the appropriate local for the duration of the request.
@@valpuia604 No problem! If you're managing the users in your app, you can add a default locale to you user table, and then if a client doesn't send up the Accept-Language header, you can perform an auth check and hydrate the locale for the request based on the user profile in the DB. Just make sure you send back the Content-Language header when you're dynamically changing the request language based on user profile or request header.
Thank you for this insight. Here what I use for versioning. // V1 Route for the app Route::prefix('v1')->group(function () { require __DIR__.'/v1/api.php'; }); What do you think? I add this inside api.php file. Should I move it to routeServiceProvider?
I probably will have to shoot a separate video. But here's the code in short: // bootstrap/app.php: return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', api: __DIR__.'/../routes/api.php', commands: __DIR__.'/../routes/console.php', health: '/up', apiPrefix: 'api/v1', then: function () { Route::middleware('api') ->prefix('api/v2') ->group(base_path('routes/api_v2.php')); }, )
HI nice tutorial, can you show me if we have some diff logic in observer or sevice in several version, i can image when use controller, but i can imagine using model versioning. eg v1 just have basic crud v2 with observer, scope and more is awesome if laravel daily team make video about this thanks
Saw this at the right time. Planning out my next project and I hadn't thought about versioning the API. thank you for this video!
Using git branching is another way to handle versioning from higher order. Having less complexity, duplicate and logical problems
How about on routes have a folder api/ then place v1.php, v2.php etc inside… then require those files in the default routes/api.php
I have done this, never do this. This will be such a difficult way to find endpoints if you're working with a lot of different endpoints.
Thanks for the valuable knowledge
why I should duplicate the code in the second version api, I was working on a graduation project with the same idea and I not duplicated the code I still call the first api functions in case I not have a plan for adding a new actions, but in case I have a new actions I use your approach.
Hello sir what will be the best way to send language (locale) in API.. 1. params (?lang=en) 2. url (url/en/abc) or others?
Your personal preference, no single best way
We store preferred language in local storage and map it on every request to the Accept-Language header. We then parse than Header in a middleware and use the appropriate local for the duration of the request.
@@pathemamike I've never knew about Accept-Language.. This will be a game changer.. thanks for the heads up
@@valpuia604 No problem! If you're managing the users in your app, you can add a default locale to you user table, and then if a client doesn't send up the Accept-Language header, you can perform an auth check and hydrate the locale for the request based on the user profile in the DB. Just make sure you send back the Content-Language header when you're dynamically changing the request language based on user profile or request header.
Thank you for this insight.
Here what I use for versioning.
// V1 Route for the app
Route::prefix('v1')->group(function () {
require __DIR__.'/v1/api.php';
});
What do you think? I add this inside api.php file. Should I move it to routeServiceProvider?
You could just move it to route service provider.
Whatever works for you :)
Hi Povilas, hope you're doing well. What about api versioning in laravel 11?
I probably will have to shoot a separate video. But here's the code in short:
// bootstrap/app.php:
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
apiPrefix: 'api/v1',
then: function () {
Route::middleware('api')
->prefix('api/v2')
->group(base_path('routes/api_v2.php'));
},
)
@@LaravelDaily thank you so much
How can you post it on the exact time when I needed it the most ?
Theory of probability. Out of 125k subscribers, there's a probability that someone of them need a certain topic :)
HI nice tutorial, can you show me if we have some diff logic in observer or sevice in several version, i can image when use controller, but i can imagine using model versioning.
eg
v1 just have basic crud
v2 with observer, scope and more
is awesome if laravel daily team make video about this
thanks
Not sure I fully understand your example. Please give more details and email me the exact example povilas@laraveldaily.com
Can you explain us how Laravel routing system how it work underhood!
What exactly is unclear for you there? You can look at the source of Laravel on GitHub and see for yourself.