Javascript 编码url并在iframe中显示

Javascript 编码url并在iframe中显示,javascript,laravel,iframe,urlencode,Javascript,Laravel,Iframe,Urlencode,我需要在我自己的页面上的iframe中显示一些url。。。所以我写: Route::get('/preview/{url}', 'ArticlesController@preview'); 我的控制器功能: public function preview($url) { $url = urlencode($url); return view('pages.preview', compact('url')); } 和offcource我的刀

我需要在我自己的页面上的iframe中显示一些url。。。所以我写:

Route::get('/preview/{url}', 'ArticlesController@preview');
我的控制器功能:

public function preview($url) {

        $url = urlencode($url);
            return view('pages.preview', compact('url'));


    }
和offcource我的刀片预览页面(javascript):

函数预览(){
函数自动调整大小(id){
var newheight;
新宽度;
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内容=“”;
var newNode=document.createElement(“DIV”);
newNode.innerHTML=内容;
document.body.appendChild(newNode);
};
预览();
现在,当我尝试以下方法时:

我得到:

找不到请求的资源/preview/http%3A%2F%2Fwww.dubaimajestic.com在此服务器上找不到%2F


如何让它工作?有什么想法吗?

这是因为
http://www.dubaimajestic.com
中有斜线,无法与laravel路由器一起正常工作

您可以使用来覆盖此行为,如下所示:

Route::get('preview/{url}', 'ArticlesController@preview')->where('url', '(.*)');
http://localhost:8888/preview?url=http://www.dubaimajestic.com
这应该是可行的:

public function preview($url) {
    dd($url);
}
但是,我会换一种方式,因为在我看来,它有点干净:

Route::get('preview', 'ArticlesController@preview');
将您的url格式化为:

您可以在控制器中这样阅读:

public function preview(Request $request) {
    dd($request->input('url'));
} 
public function preview() 
{
    $url = request()->url;

    return view('pages.preview', compact('url'));
}

/
让拉威尔认为这是路径的一部分

我建议将URL设置为如下查询字符串参数:

Route::get('preview/{url}', 'ArticlesController@preview')->where('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(Request $request) {
    dd($request->input('url'));
} 
public function preview() 
{
    $url = request()->url;

    return view('pages.preview', compact('url'));
}
这应该行得通