19

Currently I can get a route in a controller by injecting it into the method I want to use it in.

<?php namespace App\Http\Controllers;

use Illuminate\Routing\Route;

class HomeController extends Controller
{
    public function getIndex(Route $route)
    {
        echo $route->getActionName();
    }
}

However I'm trying to perform something similar in middleware but can't get it going.

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Routing\Route;
use Illuminate\Contracts\Routing\Middleware;

class SetView implements Middleware {

    protected $route;

    public function __construct(Route $route)
    {
        $this->route = $route;
    }

    public function handle($request, Closure $next)
    {
        echo $this->route->getActionName();

        return $next($request);
    }
}

Getting an error.

Unresolvable dependency resolving [Parameter #0 [ <required> $methods ]] in class Illuminate\Routing\Route

Not really sure what to do with that. Don't really care if it's a route or not, but need to get that action name somehow.

2 Answers 2

35

Remove your constructor/put it to default like;

public function __construct(){}

Try accessing the route via the handle method like so;

 $request->route();

So you should be able to access the action name like so;

 $request->route()->getActionName();

If the route return is null be sure you have registered the middleware within the App/Http/Kernel.php, like so;

protected $middleware = [
    ...
    'Path\To\Middleware',
];

The above is for global middleware

For route specific filtering place the 'Path\To\Middleware', within the middleware array within RouteServiceProvider.php within the App\Providers folder.

You can also access the route object via app()->router->getCurrentRoute().

Edit:

You could possibly try the following;

$route = Route::getRoutes()->match($request);
$route->getActionName();

This get the route from the RouteCollection. Be sure to encapsulate this within a try catch as this will throw a NotFoundHttpException.

Sign up to request clarification or add additional context in comments.

9 Comments

Not working, complains about namespace, so I made it \Route::current() but it's a big object, not sure how to pull anything out of there as getActionName() throws an error. Also strange, even though I can set properties in the __construct, when I set $this->route = \Route::current() in the construct and print_r in the handle func nothing shows up. Although if I print_r in the __construct I get the big object printing out.
Still nodda, $request->route() just comes back as null.
Have you added your middleware into the App/Http/Kernal middleware array variable or within the RouteServiceProvider? As this could be the cause of the null on the $request->route().
You would also try accessing it via app()->router->getCurrentRoute()->getActionName()
Works in the RouteServiceProvider, which is fine for now, thanks :-)
|
14

For Laravel 5.1.x

In your global middleware

use Illuminate\Support\Facades\Route;

$route = Route::getRoutes()->match($request);
$currentroute = $route->getName();

2 Comments

The only issue with this solution is the inefficient overhead of matching the route twice on every request (because the mainline router has to match it afterwards anyway).
Getting error Call to a member function match() on array (500 Internal Server Error)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.