How To Get Current Controller Name In View Laravel?

If you require to get current controller name in your view file or in your middleware or your serviceprovider etc. you can get your controller details from current route like UserController, HomeController ect. you can also get full path of controller file.
you can simple get current controller name in laravel 6, laravel 7 and laravel 8 project.
Laravel getAction() through we can get whole array of current route details, in this example i get current controller in my auth middleware, if you require on your view file or other place then you can simply get.
So, let's see example how you can get controller name in auth middleware.
Example: app/Http/Middleware/Authenticate.phpnamespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; class Authenticate { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $guard * @return mixed */ public function handle($request, Closure $next, $guard = null) { $routeArray = app('request')->route()->getAction(); $controllerAction = class_basename($routeArray['controller']); list($controller, $action) = explode('@', $controllerAction); print_r($controller); exit; if (Auth::guard($guard)->guest()) { if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { return redirect()->guest('login'); } } return $next($request); } }
I hope it cab help you...
Divyang Vadodariya
My name is Divyang Vadodariya. I'm a full-stack developer, entrepreneur and owner of RvSolutionStuff. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap ,etc from the early stage.

We are Recommending you
- How To Get Previous Month From Date In PHP?
- How To Select with Sum Query In Laravel 8
- How To Custom Auth Register Method In Laravel 8?
- How To Create Middleware With Parameters In Laravel 8
- How To Get Yesterday Date In PHP Example?
- Send Mail Using Gmail SMTP Server Laravel 8 Example.
- How To Create Login System Using C Example
- Solved - Gulp Error Cannot Find Module Laravel-elixir
- How to Get Size of Directory in MB Laravel?
- How To Get Current Controller Name In View Laravel?
Copyright © 2023 www.RVSolutionStuff.com • All Rights Reserved