我有风景mypage.blade.php和路线。
网址就像:https://example.com/mypage/param1/param2。路由使用param1并param2生成页面。
问题1
在该页面中,我尝试获取其 HTML 代码。有办法做到吗?我尝试过render(),但没有得到我想要的。
问题2
在视图中,我可以通过指定路径来获取其他视图的HTML代码吗?
你的想法是正确的。不知道为什么它对你不起作用。
在控制器中,将视图设置为变量:
$view = view('myBaseView', compact('people', 'places', 'things'));
Run Code Online (Sandbox Code Playgroud)
现在,如果转储渲染的视图变量,您将获得该页面的 HTML:
dd($view->render());
Run Code Online (Sandbox Code Playgroud)
要通过指定路径并使用内部控制器来获取另一个视图的 html,您需要设置某种包装器或 catch,以便视图变量不会作为视图返回,而是呈现为如上所述的 html。您的方法需要捕获原始控制器在推出视图之前发送的任何内容。
当然,如果你的服务器设置允许这样做,老式的 php 也有可能获取其他页面渲染的 html:
$html = file_get_contents('http://mypage.com/');
Run Code Online (Sandbox Code Playgroud)
您可能会发现 Laravelsections 方法很方便。如果您只想渲染页面的一部分,您可以通过从部分视图中调用您想要的任何部分来实现:
$sections = $view->renderSections(); // returns an associative array of 'content', 'pageHeading' etc
dd($sections['modalContent']); // this will only dump whats in the content section
Run Code Online (Sandbox Code Playgroud)
我不知道你想用这个 html 做什么,但是如果你想在页面上显示它,一旦你发送它(你可能想要返回视图以及变量 $view.. .作为普通变量(如果是这样),请记住使用以下格式:
{!! $view !!}
Run Code Online (Sandbox Code Playgroud)
华泰