我想知道是否有一种方法可以在响应中列出加载/包含/扩展的所有视图?
我知道laravel-debugbar,但出于单元测试的目的,我想在我的代码中这样做。
我只是想澄清一下:我并不打算列出我的资源文件夹中的所有视图。我想要获取当前响应/请求中使用的所有视图的列表。
谢谢!
发布于 2015-09-24 07:45:55
我也为自己做过类似的事情。这并不完美。使用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>';
}
});
此函数的作用是检查在变量$full_path中定义的视图路径是否存在,并扫描该目录中的视图文件。现在,list-views
将列出所有可用的视图。
发布于 2018-06-07 05:50:21
laravel debugbar可以做到这一点,但在我的例子中,我找不到一种方法来一次列出同一个url下的所有视图,所以如果现在有人知道我会多么感激它。
// 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]);
});
另一种方法是,但这将只显示主视图“而不是嵌套的或父的”
// 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]);
}
});
您也可以从中间件获取视图,但这将只显示与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;
}
https://stackoverflow.com/questions/32750533
复制相似问题