Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lumen - Routing with Prefix and Optional Parameter

I'm looking at routing in Lumen and it doesn't appear to be working correctly and I can't work out if it's an issue or my understanding.

$router->get('{adaptor}[/{id}]',     ['uses' => 'MyController@readAction']);

This way works, but I'd prefer to use a prefix to group all my routes:

$router->group(['prefix' => '{adaptor}'], function () use ($router) {
        $router->get('[/{id}]',      ['uses' => 'MyController@readAction']);
       // CRUD routes to be added here
});

If I go to /acme I get MethodNotAllowed Exception, but if I add /acme/1 it works fine. Does a prefix NEED another route item for it to work?

like image 992
Farkie Avatar asked Feb 12 '19 12:02

Farkie


People also ask

What would be the correct way to declare a route with an optional parameter name?

The route: { path: 'list/:name/:type', loadChildren: './list/list. module#ListModule'}, The :name parameter is required for this route to load correctly, but the :type parameter is optional.

What are the parameters of routing?

Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req. params object, with the name of the route parameter specified in the path as their respective keys.

What is route prefix in laravel?

Route Prefixes The prefix method may be used to prefix each route in the group with a given URI. For example, you may want to prefix all route URIs within the group with admin : Route::prefix('admin')->group(function () { Route::get('/users', function () { // Matches The "/admin/users" URL.


1 Answers

A prefix will need another route item in order for it to work correctly.

https://lumen.laravel.com/docs/5.4/routing#route-parameters

Optional parameters are only supported in a trailing position of the URI.

like image 103
jtlad Avatar answered Nov 15 '22 09:11

jtlad