Featured image of post RESTful Routing URL Naming Conventions

RESTful Routing URL Naming Conventions

Understanding RESTful-style APIs through Laravel's routing implementation

  • A previous blog briefly introduced Using RESTful in Laravel
  • This article explains various RESTful-style route writing conventions. While not necessarily authoritative, these patterns are semantically meaningful in practice.

Base Routing

  • Typically grouped with a common prefix:
    • Always follows API + version format
    • /api/v1

Basic Parameters

  • Pagination parameters using QueryString:
    • limit
    • page
    • Example: /url?limit=10&page=1
  • Multi-parameter sorting:
    • Example: /url?desc=created_at,id&asc=grade,updated_at

Fundamental Routes

  • List categories:
    • method: GET
    • /categories
  • Create category:
    • method: POST
    • /categories
  • Delete category:
    • method: DELETE
    • /categories/{category}
  • Get single category:
    • method: GET
    • /categories/{category}
  • Update category:
    • method: PUT
    • /categories/{category}

Intermediate Routes

  • Get articles under category:
    • method: GET
    • /categories/{category}/articles
  • Get articles with tag:
    • method: GET
    • /tags/{tag}/articles
  • Batch delete articles (avoid when possible):
    • method: DELETE
    • /articles/batch?id=1,2,3

Authentication Routes

  • Common implementations:

  • Login:

    • method: POST
    • /login
  • Logout:

    • method: POST
    • /logout
  • RESTful-compliant alternatives:

  • Create token (login):

    • method: POST
    • /tokens
  • Delete token (logout):

    • method: DELETE
    • /tokens/{token}

Multi-word Route Handling

  • For SEO-friendly multi-word routes:
    • Use hyphen-separated format
    • Get articles by type:
    • method: GET
    • /article-types/{article_type}/articles