【问题标题】:laravel file_get_contents of a route get no responselaravel file_get_contents 路由没有响应
【发布时间】:2015-01-14 14:39:22
【问题描述】:

我有一条路线:

Route::group(array('prefix' => 'playlist'), function()
{
   Route::get('raw/{id}', array('as'=>'rawPlaylist', 'uses'=>'PlaylistsController@raw'));
});

当我打开网址时:

http://localhost:8000/playlist/raw/1

一切都很好,页面将加载并向我显示视图。

在控制器中我得到 URL:

$url = URL::route('rawPlaylist', 1);

问题: 我想读取视图并将整个视图存储到一个变量中:

$html = file_get_contents($url);

echo $url 是http://localhost:8000/playlist/raw/1 这是对的。

但这不起作用。我没有得到任何回应,并且 php artisan 服务中断。 为什么?这里有什么问题?

【问题讨论】:

  • 请看 url echo $url; ?
  • echo $url 是http://localhost:8000/playlist/raw/1 这是对的。
  • 不行:看你的网址localhost:8000/de/playlist/raw/1
  • 不断调用同一个url。 file_get_contents($url) 内容持久化连接您的服务器。
  • 是的,你是对的。我现在将路线更改为 localhost:8000/playlist/raw/1 .. 而不是 /de/ 。它现在是相同的 URL,但它也不起作用......

标签: php laravel file-get-contents


【解决方案1】:

您不能在 laravel 路由上使用 file_get_contents,因为 laravel 路由不是视图文件的实际路径。

您可以知道,因为您的视图没有存储在文件夹de/playlist/raw 中,而是存储在app/views/pathtoyourrawview 中。

我还没有尝试过,但我假设您可以使用以下方法将视图的代码捕获到变量中:

$html = View::make('rawPlaylist')->render();

基本上,不是返回您的View::make 结果,而是将其写入变量。

【讨论】:

  • $html = View::make('rawPlaylist')->render(); 以确保返回实际的 HTML 而不是视图对象 ;)
  • 啊,感谢@Dencker 的澄清。我会更新我的答案。
  • 没问题。很高兴我们能提供帮助!
猜你喜欢
  • 2013-09-10
  • 2015-07-23
  • 1970-01-01
  • 1970-01-01
  • 2019-03-21
  • 2016-08-27
  • 2017-01-23
  • 2015-02-21
  • 2016-07-20
相关资源
最近更新 更多