php - Laravel get view name of rendered route

971

Is here a way to achieve that : I want to check in AppServiceProvider, name of rendered view.

So, for example :

  1. User enters at/home, for that route in controller itreturn view('website.home');
  2. From AppServiceProvider get current route, and get view name that is rendered.
664

Answer

Solution:

I don't know that it's possible to get the view name in theAppServiceProvider, as that runs prior to a view being created in the route's respective controller method. You can get the name of the view within the controller, however, after it's been created:

$view = view('website.home');
$name = $view->getName();
return $view;

The current route name is accessible within the service provider using:

request()->route()->getName();

// or

Request::getCurrentName();

edit

Actually this would probably be doable with a view composer and a wildcard match on the view:

// AppServiceProvider boot method
public function boot()
{
    view()->composer('*', function($view) {
        view()->share('viewName', $view->getName());
    });
}

People are also looking for solutions to the problem: php - Joomla - How to send data from Sub controller to Model?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.