iRazaSyed's avatar

Lumen - Get Route Parameters Value.

Here's a simple snippet that gives access to route parameter's value.

This is present in Laravel but was missing in Lumen. Would be good if it's integrated to the core but until that happens, this works fine!

<?php // app/helpers.php
if (!function_exists('route_parameter')) {
    /**
     * Get a given parameter from the route.
     *
     * @param $name
     * @param null $default
     * @return mixed
     */
    function route_parameter($name, $default = null)
    {
        $routeInfo = app('request')->route();

        return array_get($routeInfo[2], $name, $default);
    }
}

Create a app/helpers.php file and then load this in composer by adding it under autoload. Ex:

"autoload": {
    "psr-4": {
        "App\\": "app/"
    },
    "classmap": [
        "database/"
    ],
    "files": [
      "app/helpers.php"
    ]
},

Make sure to composer dump-autoload. ($ composer du).

0 likes
7 replies
crishoj's avatar

Accessing app('request')->route() works fine under normal circumstances.

However, when the request comes from a Laravel\Lumen\Testing\TestCase using the $this->post(...)->seeStatusCode(...) syntax, app('request')->route() is empty.

iRazaSyed's avatar

@carbontwelve No problem :)

You should check out my Larasupport package as well. It has all these functions.

1 like
carbontwelve's avatar

@iRazaSyed haha, yes; as it happens I have been recommending that package to other people who have had similar functionality requirements within Lumen.

jimmck's avatar

@iRazaSyed Nice work. I made a post a couple days ago about Views just being removed from Lumen. I love the response to your request, Use Laravel. This arrogant nonsense has made me check out Zend. As a Java programmer its a model I am well used to. They have a true interface model. Its a bit more terse than Laravel, but it seems very configurable. And its open source. I hesitated because I thought it was not open and fell into Laravel when I started to check out PHP. The Laravel codebase is just too unstable to consider for production scale apps in my opinion. The intentions are good the execution is becoming suspect to me. I have seen this before and its not been good. Hopefully the developers note your efforts and others like you.

Please or to participate in this conversation.