Tuesday, July 11, 2017

Laravel 5: Get Controller Name in View | Route Details | Controller & Action Name | Route Parameters

You can create a view composer that injects those variables into your view file. In "app/Providers/AppServiceProvider.php" add something like this:

public function boot()
{
    //app('view')->composer("folder.specific_view_name", function ($view) {
    app('view')->composer("*", function ($view) {
        $action = app("request")->route()->getAction();

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

        list($controller_name, $action_name) = explode("@", $controller);

        $view->with(compact("controller_name", "action_name"));
    });
}

You also can parameters from current request as below:

app('request')->route()->parameters()

Array
(
    [middleware] => web
    [as] => folder.view_name
    [uses] => App\Http\Controllers\UserController@show
    [controller] => App\Http\Controllers\UserController@show
    [namespace] => App\Http\Controllers
    [prefix] => 
    [where] => Array
        (
        )


)


No comments:

Post a Comment