onlinecode

How to Get Current Controller Name in View Laravel?

How to Get Current Controller Name in View Laravel?

In this post we will give you information about How to Get Current Controller Name in View Laravel?. Hear we will give you detail about How to Get Current Controller Name in View Laravel?And how to use it also give you demo for it if it is necessary.

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.

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

Also see:Laravel – How to get current URL in controller or view?

namespace AppHttpMiddleware;


use Closure;

use IlluminateSupportFacadesAuth;


class Authenticate

{

/**

* Handle an incoming request.

*

* @param IlluminateHttpRequest $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);

}

}

Hope this code and post will helped you for implement How to Get Current Controller Name in View Laravel?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version