编码 url 并在 iframe 中显示
Encode url and show in iframe
我需要在我自己的页面的 iframe 中显示一些 url...所以我写:
Route::get('/preview/{url}', 'ArticlesController@preview');
我的控制器功能:
public function preview($url) {
$url = urlencode($url);
return view('pages.preview', compact('url'));
}
并关闭我的 blade 预览页面 (javascript):
function preview(){
function autoResize(id){
var newheight;
var newwidth;
if(document.getElementById){
newheight = document.getElementById(id).contentWindow.document .body.scrollHeight;
newwidth = document.getElementById(id).contentWindow.document .body.scrollWidth;
}
document.getElementById(id).height = (newheight) + "px";
document.getElementById(id).width = (newwidth) + "px";
};
var content = '<iframe id="iframe2" src="{{$url}}" style="border:0px #FFFFFF none; position: relative;left: 0px;width: 100%; height:100%; top: 0;" name="myiFrame1" scrolling="yes" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%" onLoad="autoResize(iframe1);"></iframe>';
var newNode = document.createElement("DIV");
newNode.innerHTML = content;
document.body.appendChild(newNode);
};
preview();
现在,当我尝试类似的操作时:
http://localhost:8888/preview/http%3A%2F%2Fwww.dubaimajestic.com%2F
或
http://localhost:8888/preview/http://www.dubaimajestic.com
我得到:
未找到请求的资源 /preview/http%3A%2F%2Fwww.dubaimajestic.com%2F 在此服务器上未找到。
如何让它工作?有什么想法吗?
这是因为 http://www.dubaimajestic.com
中有斜线,不能与 laravel 路由器一起正常工作。
您可以使用 Regular Expression Constraints 覆盖此行为,如下所示:
Route::get('preview/{url}', 'ArticlesController@preview')->where('url', '(.*)');
这应该有效:
public function preview($url) {
dd($url);
}
但是我会切换到不同的方式,因为在我看来它更干净一些:
Route::get('preview', 'ArticlesController@preview');
将您的 url 格式化为:
http://localhost:8888/preview?url=http://www.dubaimajestic.com
您可以在控制器中像这样阅读:
public function preview(Request $request) {
dd($request->input('url'));
}
/
使 Laravel 认为这是路径的一部分。
我建议将 URL 作为查询字符串参数,如下所示:
http://localhost:8888/preview?url=http://www.dubaimajestic.com
然后在你的 routes.php
:
// Don't accept {url} as an argument
Route::get('/preview', 'ArticlesController@preview');
然后在你的控制器中:
public function preview()
{
$url = request()->url;
return view('pages.preview', compact('url'));
}
应该可以。
我需要在我自己的页面的 iframe 中显示一些 url...所以我写:
Route::get('/preview/{url}', 'ArticlesController@preview');
我的控制器功能:
public function preview($url) {
$url = urlencode($url);
return view('pages.preview', compact('url'));
}
并关闭我的 blade 预览页面 (javascript):
function preview(){
function autoResize(id){
var newheight;
var newwidth;
if(document.getElementById){
newheight = document.getElementById(id).contentWindow.document .body.scrollHeight;
newwidth = document.getElementById(id).contentWindow.document .body.scrollWidth;
}
document.getElementById(id).height = (newheight) + "px";
document.getElementById(id).width = (newwidth) + "px";
};
var content = '<iframe id="iframe2" src="{{$url}}" style="border:0px #FFFFFF none; position: relative;left: 0px;width: 100%; height:100%; top: 0;" name="myiFrame1" scrolling="yes" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%" onLoad="autoResize(iframe1);"></iframe>';
var newNode = document.createElement("DIV");
newNode.innerHTML = content;
document.body.appendChild(newNode);
};
preview();
现在,当我尝试类似的操作时:
http://localhost:8888/preview/http%3A%2F%2Fwww.dubaimajestic.com%2F
或
http://localhost:8888/preview/http://www.dubaimajestic.com
我得到:
未找到请求的资源 /preview/http%3A%2F%2Fwww.dubaimajestic.com%2F 在此服务器上未找到。
如何让它工作?有什么想法吗?
这是因为 http://www.dubaimajestic.com
中有斜线,不能与 laravel 路由器一起正常工作。
您可以使用 Regular Expression Constraints 覆盖此行为,如下所示:
Route::get('preview/{url}', 'ArticlesController@preview')->where('url', '(.*)');
这应该有效:
public function preview($url) {
dd($url);
}
但是我会切换到不同的方式,因为在我看来它更干净一些:
Route::get('preview', 'ArticlesController@preview');
将您的 url 格式化为:
http://localhost:8888/preview?url=http://www.dubaimajestic.com
您可以在控制器中像这样阅读:
public function preview(Request $request) {
dd($request->input('url'));
}
/
使 Laravel 认为这是路径的一部分。
我建议将 URL 作为查询字符串参数,如下所示:
http://localhost:8888/preview?url=http://www.dubaimajestic.com
然后在你的 routes.php
:
// Don't accept {url} as an argument
Route::get('/preview', 'ArticlesController@preview');
然后在你的控制器中:
public function preview()
{
$url = request()->url;
return view('pages.preview', compact('url'));
}
应该可以。