petrusko's avatar

Lumen routing (any)

I/we have a laravel 4.2 implementation in some php application; Now we decided that we're going to upgrade and migrate to lumen 5.2; as we were using laravel just for routing.

Is there a replacable code for: Route::any('/{controller}/{action?}/{params?}', function($controller, $action = 'index', $params = '')

Petrusko

0 likes
2 replies
petrusko's avatar

After waiting for answer and searching around the web and digging through code I've managed to configure router like this

$app->post('/system-api/{controller}[/{action}[/{params}]]', function ($controller, $action = 'index', $params = '') use ($app) { $app->get('/system-api/{controller}[/{action}[/{params}]]', function ($controller, $action = 'index', $params = '') use ($app) {

Besides that this looks bit "redundant", I might need replacement for "any" method.

1 like
KingBeto's avatar

Hi, Lumen does not have this $app->any() method implemented.

You can create a Group like this.


$router->group(['prefix' => 'this-is-a-prefix'], function () use ($router) {

    $controller = 'MyModelController@whateverMethod';

    $router->get('/{route:.*}/', $controller);
    $router->post('/{route:.*}/', $controller);
    $router->put('/{route:.*}/', $controller);
    $router->patch('/{route:.*}/', $controller);
    $router->delete('/{route:.*}/', $controller);

});

This code will catch All methods for this url:

http:://your_lumen_implementation_url/this-is-a-prefix/whatever/comes/here/1

Where "whatever/comes/here/1" will be passed to the MyModelController@whateverMethod as a variable like this:

use Laravel\Lumen\Routing\Controller as BaseController;

class MyModelController extends BaseController
{
    public function whateverMethod(Request $request, $route) {
        // here $route will contain this value "whatever/comes/here/1"
        // if you want to know what method was posted, use $request->method()
    }
}
1 like

Please or to participate in this conversation.