frdmn's avatar

Lumen - current route in view

Hello,

I try to display the current route name in a view and was using something along the lines {{ Route::getCurrentRoute()->getActionName() }} and in my routes.php:

use Illuminate\Support\Facades\Route;

However, this throws the following error:

Fatal error: Call to undefined function getCurrentRoute() 

Can someone point me in the right direction?

0 likes
8 replies
frdmn's avatar

Ah, that explains the undefined function error. I can't find a method in nikic/FastRoute, though. Is there an Lumen function I can use instead?

bashy's avatar

Is there a reason you need to use that method? Probably a sign you're using something that is too much for base Lumen setup

bestmomo's avatar

Lumen application set a $currentRoute property but it's protected and there is no getter. But you can get current route like this :

$method = Request::getMethod();
$pathInfo = Request::getPathInfo();
$route = $app->getRoutes()[$method.$pathInfo];

And for action :

$app->getRoutes()[$method.$pathInfo]['action']
frdmn's avatar

@bestmomo Thank you, but $app->getRoutes() is empty for me. I tried adding the code in the routes.php since I don't really use the Controller at all the moment.

@bashy I actually just want to add the subtitle in the <title> in my blade view based on the current route name. Not sure but perhaps there's a better approach to do so?

frdmn's avatar

@bestmomo My route looks like:

$app->get('/', function() use ($app) {
    return view('pages.home', $app->settings);
});
lucasctd's avatar

I know it's too old but, if somebody else is looking for the same thing, they can acchieve it as shown below

//Lumen 5.8 (I am inside a middleware, that's way I have "$request" as a variable)
app()->router->getRoutes()[$request->method() . $request->getPathInfo()]

Please or to participate in this conversation.