Skip to content

Conversation

@vzz3
Copy link

@vzz3 vzz3 commented Apr 11, 2023

Issue

Problem 1

Routing slugs are treated as absolute paths and not relative to the site configuration URL.
Consider the following example
* base URL in the TYPO3 site config: http://localhost:7080/rest
* path of a route in ext/Configuration/Routes.yml: api/v1/login

Requesting just http://localhost:7080/api/v1/login will not work since TYPO3 would not be able to find the site configuration (TYPO3\CMS\Frontend\Middleware\SiteResolver).
On the other hand requesting http://localhost:7080/rest/api/v1/login will lead to the problem that LMS\Routes\Extbase\RouteHandler will not be able to match the route, since it tries to match the entire requested path (rest/api/v1/login) and not only the path below the site base path (rest), which would only be (api/v1/login).

Problem 2

The prefix api of the route path is hardcoded in LMS\Routes\Routing\RestApiEnhancer. Therefore, all routes in any Routes.yml that does not start with api/ will never work.

Improvements in this Patch

This patch adds the possibility to overwrite the prefix api of the route path in LMS\Routes\Routing\RestApiEnhancer with the routeEnhancers configuration pathPrefix. This, makes it possible to configure routes in a Routes.yml, that do not need to beginn with api/.
Furthermore, the site base path is taken into account wen matching routes in the LMS\Routes\Extbase\RouteHandler. This makes it possible to use this extension on sites that does not have an empty site base path in the TYPO3 site config (e.g. http://localhost:7080/rest)

Possible sample configuration:

  • Endpoint: http://localhost:7080/rest/pos/api/v1/login
  • TYPO3 site config example:
    base: 'http://localhost:7080/rest'
    routeEnhancers:
        ApplyRoutesCollection:
            type: Routes
            # added functionality:
            pathPrefix: 'pos/api'
    
  • Routes.yml example:
     api_access-login:
         # path does not need to start with api anymore
         path: pos/api/v1/login
         controller: VENDOR\myext\Controller\AccessController::login
         methods: [POST]
         format: json
         defaults:
             vendor: VENDOR
             extension: myext
             plugin: Api
    

…route path in *LMS\Routes\Routing\RestApiEnhancer* with the routeEnhancers configuration *pathPrefix*. This, makes it possible to configure routes in a *Routes.yml*, that do not need to beginn with *api/*.

Furthermore, the site base path is taken into account wen matching routes in the *LMS\Routes\Extbase\RouteHandler*. This makes it possible to use this extension on sites that does not have an empty site base path in the  TYPO3 site config (e.g. *http://localhost:7080/rest*)

Possible sample configuration:
* Endpoint: http://localhost:7080/rest/pos/api/v1/login
* TYPO3 site config example:
    base: 'http://localhost:7080/rest'
    routeEnhancers:
        ApplyRoutesCollection:
            type: Routes
            # added functionality:
            pathPrefix: 'pos/api'

* Routes.yml example:
    api_access-login:
        # path does not need to start with api anymore
        path: pos/api/v1/login
        controller: VENDOR\myext\Controller\AccessController::login
        methods: [POST]
        format: json
        defaults:
            vendor: VENDOR
            extension: myext
            plugin: Api
@cepheiVV
Copy link

The base URL problem also occurs with a language setup that uses prefixes in the URL like base: /en/:

languages:
  -
    title: English
    enabled: true
    base: /en/
    typo3Language: default
    locale: en_GB.UTF-8
    iso-639-1: en
    websiteTitle: medbase
    navigationTitle: EN
    hreflang: en-GB
    flag: gb
    direction: ltr
    languageId: 0

This PR doesn't solve the base URL prefix problem for language setups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants