How To Implement Laravel API V1/V2 Versioning

Поделиться
HTML-код
  • Опубликовано: 29 авг 2024

Комментарии • 23

  • @BryanSchmidtSchmidty
    @BryanSchmidtSchmidty Год назад +1

    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!

  • @Poooya
    @Poooya Год назад +3

    Using git branching is another way to handle versioning from higher order. Having less complexity, duplicate and logical problems

  • @sibow
    @sibow Год назад +2

    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

    • @epicgameryt4052
      @epicgameryt4052 Год назад +2

      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.

  • @basecoinbasecoin7123
    @basecoinbasecoin7123 Год назад

    Thanks for the valuable knowledge

  • @mmramadan496
    @mmramadan496 Год назад

    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.

  • @valpuia604
    @valpuia604 Год назад +1

    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?

    • @LaravelDaily
      @LaravelDaily  Год назад +1

      Your personal preference, no single best way

    • @pathemamike
      @pathemamike Год назад +1

      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
      @valpuia604 Год назад +1

      @@pathemamike I've never knew about Accept-Language.. This will be a game changer.. thanks for the heads up

    • @pathemamike
      @pathemamike Год назад +1

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

  • @debjit21
    @debjit21 Год назад

    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?

    • @devhammed
      @devhammed Год назад

      You could just move it to route service provider.

    • @LaravelDaily
      @LaravelDaily  Год назад +1

      Whatever works for you :)

  • @umidkurbanov5495
    @umidkurbanov5495 5 месяцев назад

    Hi Povilas, hope you're doing well. What about api versioning in laravel 11?

    • @LaravelDaily
      @LaravelDaily  5 месяцев назад +1

      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'));
      },
      )

    • @umidkurbanov5495
      @umidkurbanov5495 5 месяцев назад

      @@LaravelDaily thank you so much

  • @hammadzafar2674
    @hammadzafar2674 Год назад +1

    How can you post it on the exact time when I needed it the most ?

    • @LaravelDaily
      @LaravelDaily  Год назад +1

      Theory of probability. Out of 125k subscribers, there's a probability that someone of them need a certain topic :)

  • @zulfikarditya7142
    @zulfikarditya7142 Год назад

    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

    • @LaravelDaily
      @LaravelDaily  Год назад

      Not sure I fully understand your example. Please give more details and email me the exact example povilas@laraveldaily.com

  • @lahoucineoldakdim7778
    @lahoucineoldakdim7778 Год назад

    Can you explain us how Laravel routing system how it work underhood!

    • @LaravelDaily
      @LaravelDaily  Год назад

      What exactly is unclear for you there? You can look at the source of Laravel on GitHub and see for yourself.