0

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..?

Amit Kumar PRO
  • 1,222
  • 2
  • 15
  • 27
Mr.Singh
  • 1,421
  • 6
  • 21
  • 46
  • Hm i never used that one to inject, you can use `$request->route()` instead, its injected using `(\Illuminate\Http\Request $request)`. – Flame Jan 30 '20 at 19:29
  • can you explain what is output expect from this ? – Hamelraj Jan 30 '20 at 20:16
  • Just curious, why you need change `$this->controller` and `$this->action`? – Wahyu Kristianto Jan 30 '20 at 21:09
  • @WahyuKristianto, I am passing these values in every controller/action and use them to make the link(s) active in the administrator area. – Mr.Singh Jan 31 '20 at 04:37
  • @Hamelraj, This code is placed in every controller to extract the controller and action name. – Mr.Singh Jan 31 '20 at 04:39
  • 1
    You can use router name, eg `{{ Route::currentRouteName() == 'route.name' ? 'active' : '' }}`. – Wahyu Kristianto Jan 31 '20 at 07:16
  • @WahyuKristianto, I will try this, thank you. – Mr.Singh Jan 31 '20 at 17:29
  • @WahyuKristianto, Thank for the solution. It worked. However, I found one more solution which also works very well and fits quite nicely with my existing code without the need to make too many changes. Here is the url: https://stackoverflow.com/questions/29549660/get-laravel-5-controller-name-in-view. But still thank you very much for helping me. :) – Mr.Singh Jan 31 '20 at 19:02
  • Glad to hear it @Pummi – Wahyu Kristianto Jan 31 '20 at 21:04

0 Answers0