dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.33k stars 1.25k forks source link

route alias does not when used with name() function #1695

Closed christoph-kluge closed 4 years ago

christoph-kluge commented 4 years ago
Q A
Bug? no yes
New Feature? no yes
Framework Laravel
Framework version 5.8.x
Package version 2.4.0
PHP version 7.3.x

Actual Behaviour

Given the following route definition:

/** @var Router $api */
$api = app(Router::class);
$api->version('v1', function (Router $api) {
    $api->get('entity/{uuid}', MyHandler::class)->name('myalias');
});

When using route('myalias') it will throw an exception Route [myalias] not defined..

Expected Behaviour

Obviously it should not throw an exception

Steps to Reproduce

See actual behavior.

Possible Solutions

After a quick check I've realised that RouteCollection->add() does the name list lookup. The problem is that ->name() is added afterwards to the route, so the route collection is not build up correctly. Refreshing the RouteCollection seems to fix the behavior.

Additional note

It will still fail when we have an exception before the Adapter->dispatch() is hit and we'll use route() during exception handling. Didn't found a better solution for now but it gives us an additional feature for now w/o breaking existing functionality.

specialtactics commented 4 years ago

Thanks for your work @christoph-kluge !