22

Our old website CSS was set up so that the body tag had an id of the controller name and a class of the action name, using Zend Framework 1. Now we're switching to Laravel 5. I found a way to get the action name through the Route class, but can't find a method for the controller name. I don't see anything in the Laravel docs like this. Any ideas?

This is how you do with action. You inject the Route class, and then call:

$route->getActionName().

I'm looking for something similar for controllers. I've checked the entire route class and found nothing.

sehummel
  • 5,476
  • 24
  • 90
  • 137

8 Answers8

53

If your layout is a Blade template, you could create a view composer that injects those variables into your layout. In app/Providers/AppServiceProvider.php add something like this:

public function boot()
{
    app('view')->composer('layouts.master', function ($view) {
        $action = app('request')->route()->getAction();

        $controller = class_basename($action['controller']);

        list($controller, $action) = explode('@', $controller);

        $view->with(compact('controller', 'action'));
    });
}

You will then have two variables available in your layout template: $controller and $action.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • 1
    I like that. It's not quite what I need, but I see what you're doing and I can adapt it. Thanks! – sehummel Apr 09 '15 at 22:29
  • @sehummel Awesome! Glad I was able to steer you in the right direction. – Martin Bean Apr 10 '15 at 07:38
  • @MartinBean I was searching this output, for automating the Entrust roles and permission. When I used `code` app('request')->route()->getAction(); `code` in middleware, I got FatalErrorException as call a member function getAction() on a non-object But, when I wrote this code into Service provider it worked? Can you help me,how to get it working in middleware!? – Tarunn May 15 '15 at 11:32
  • @MartinBean : this is the question (task) I am expecting [answer to this](http://stackoverflow.com/questions/30235639/automate-entrust-permission-with-accessing-the-route-name-parameters-and-check-f) Any help is much appreciated. – Tarunn May 15 '15 at 11:37
  • It is not working in my scenario. I am using laravel `5.1` – Vineet Aug 11 '16 at 09:05
16

I use a simple solution. You can test and use it in everywhere, also in your views:

{{ dd(request()->route()->getAction()) }}
4

I will simply use as bellow

$request->route()->getActionMethod()
vrkansagara
  • 606
  • 6
  • 13
2

To get something like PostController try following ...

preg_match('/([a-z]*)@/i', $request->route()->getActionName(), $matches);
$controllerName = $matches[1];

$matches[1] includes the first group while $matches[0] includes everything matched. So also the @ which isn't desired.

Ilario Engler
  • 2,419
  • 2
  • 27
  • 40
0

use this

strtok(substr(strrchr($request->route()->getActionName(), '\\'), 1), '@')
Fahrettin Aksoy
  • 109
  • 1
  • 5
0

To add to Martin Bean answer, using Route::view in your routes will cause the list function to throw an Undefined offset error when this code runs;

list($controller, $action) = explode('@', $controller);

Instead use this, which assigns null to $action if not present

list($controller, $action) = array_pad(explode('@', $controller), 2, null);

0

You can use this to just simply display in the title like "Customer - My Site" (laravel 9)

{{ str_replace('Controller', '', strtok(substr(strrchr(request()->route()->getActionName(), '\\'), 1), '@'))}} - {{ config('app.name') }}
Rex Bengil
  • 60
  • 3
-1

You can add this (tested with Laravel v7+)

<?php
    use Illuminate\Support\Facades\Route;

    echo Route::getCurrentRoute()->getActionMethod();
?>

or

You can use helper function

<?php echo request()->route()->getActionMethod(); ?>

for example :-

Route::get('test', [\App\Http\Controllers\ExampleController::class, 'exampleTest'])->name('testExample');

Now If I request {app_url}/test then it will return exampleTest

Harsh Patel
  • 1,032
  • 6
  • 21