I was wondering if there was a way that I could list all the views load / included / extended in a response?
I know of laravel-debugbar, but I'd like to do it from within my code for Unit Testing purposes.
Just to clarify: I'm not looking to list all of the views in my resources folder. I'd like to get a list of all views used in the current response/request.
Thanks!
I have done something similar for myself. It's not perfect. Create the following route with function:
Route::get('list-views', function(){
$full_path = 'FULL-PATH-TO-YOUR-LARAVEL-VIEWS-FOLDER'; //LIKE /home/account/www/resources/views/
if(!is_dir($full_path))
return 'Views directory not found';
$files = scandir($full_path);
unset($files[0]);
unset($files[1]);
if(($key = array_search('emails', $files)) !== false) {
unset($files[$key]);
}
foreach($files AS $file){
$link = str_replace('.blade.php','',$file);
echo '<a href="'.$link.'">'.$link.'</a>'.'<br>';
}
});
What this function does is checks if the views path exists which you define in the variable $full_path, scans that directory for view files. Now, list-views
will list all available views.
laravel debugbar does that, but in my case i cant find a way to list all the views under the same url in one go, so if anyone nows how i would deeply appreciate it.
// EventServiceProvider@boot()
app('events')->listen('composing:*', function ($view, $data = []) {
$url = url()->current();
$view = $data[0];
$name = $view->getName();
$path = ltrim(str_replace(base_path(), '', realpath($view->getPath())), '/');
logger([$url=>$path]);
});
another way could be but this will only display the main view "not the nested or parent"
// EventServiceProvider@boot()
use Illuminate\Foundation\Http\Events\RequestHandled;
app('events')->listen(RequestHandled::class, function ($event) {
$request = $event->request;
$response = $event->response;
$check = !$request->ajax() &&
!$request->pjax() &&
$request->isMethodCacheable() &&
$response->isSuccessful();
if ($check) {
$view = $response->original;
$path = ltrim(str_replace(base_path(), '', realpath($view->getPath())), '/');
logger([$request->url(), $path]);
}
});
you can also get the view from a middleware but this will only display the main view same as b4
public function handle($request, Closure $next)
{
$response = $next($request);
$check = !$request->ajax() &&
!$request->pjax() &&
$request->isMethodCacheable() &&
$response->isSuccessful();
if ($check) {
$view = $response->original;
$path = ltrim(str_replace(base_path(), '', realpath($view->getPath())), '/');
logger([$request->url(), $path]);
}
return $response;
}