In my laravel application I need to access the Route
in every controller's __construct()
.
class SomeController extends Controller
{
public function __construct(Route $route)
{
parent::__construct();
/**
* Middleware(s)
*/
$this->middleware('auth');
/**
* Extracting current controller & action from the namespace
*/
list($controller, $method) = @explode("@", $route->getActionName());
/**
* Assigning values to private variables
*/
$this->controller = preg_replace('/.*\\\/', '', $controller);
$this->action = preg_replace('/.*\\\/', '', $method);
}
}
If I run this code then it works without any error. However, when I run php artisan route:list
it gives me Illuminate\Contracts\Container\BindingResolutionException : Unresolvable dependency resolving [Parameter #0 [ <required> $methods ]] in class Illuminate\Routing\Route
.
If I change my code as follows and run the same command then it shows the list of routes..
class SomeController extends Controller
{
public function __construct()
{
parent::__construct();
/**
* Middleware(s)
*/
$this->middleware('auth');
/**
* Extracting current controller & action from the namespace
*/
// list($controller, $method) = @explode("@", $route->getActionName());
/**
* Assigning values to private variables
*/
// $this->controller = preg_replace('/.*\\\/', '', $controller);
// $this->action = preg_replace('/.*\\\/', '', $method);
}
}
I need the controller and action name on every render to show the active links.
How to fix this..?