如何解决 Laravel 5 中的超时错误

新手上路,请多包涵

我有以下设置:

在路线中,我有:

Route::get(‘articles’, ‘ArticlesController@index’);

控制器中的 index 方法很简单:

 public function index()
{
   $articles = Article::all();
   return View('articles.index', compact('articles'));
}

在视图中:

 @extends('../app')
@section('content')
<h1>Articles</h1>
<p>
    @foreach($articles as $article)
    <article>
        <h2><a href="{{action('ArticlesController@show', [$article->id])}}">{{$article->title}}</a></h2>
        <p>{{ $article->body }}</p>
    </article>
    @endforeach
</p>
@stop

我试图替换:

 $articles = Article::all();

$article = Article::latest()->get();

这样我实际上可以首先显示最新的文章。我得到了错误:

 FatalErrorException in Str.php line 322:
Maximum execution time of 30 seconds exceeded

调用堆栈是:

 in Str.php line 322
at FatalErrorException->__construct() in HandleExceptions.php line 131
at HandleExceptions->fatalExceptionFromError() in HandleExceptions.php line 116
at HandleExceptions->handleShutdown() in HandleExceptions.php line 0
at Str::snake() in helpers.php line 561
at snake_case() in ControllerInspector.php line 105
at ControllerInspector->getVerb() in ControllerInspector.php line 78
at ControllerInspector->getMethodData() in ControllerInspector.php line 39
at ControllerInspector->getRoutable() in Router.php line 251
at Router->controller() in Router.php line 226
at Router->controllers() in Facade.php line 210
at Facade::__callStatic() in routes.php line 21
at Route::controllers() in routes.php line 21
in RouteServiceProvider.php line 40

… ETC

我已将控制器方法恢复到原来的样子,但错误仍然存在。

你能告诉我如何解决这个问题吗?

原文由 Geordie Gadgie 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 940
2 个回答

超过 30 秒的最大执行时间 错误与 Laravel 无关,而与您的 PHP 配置有关。

这是您可以解决的方法。您需要更改的设置是 max_execution_time

 ;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
memory_limit = 8M      ; Maximum amount of memory a script may consume (8MB)

您可以将 max_execution_time 更改为 300 秒,如 max_execution_time = 300

您可以在 Loaded Configuration File 部分的 phpinfo 函数的输出中找到 PHP 配置文件的路径。

原文由 Noman Ur Rehman 发布,翻译遵循 CC BY-SA 3.0 许可协议

默认情况下,最大执行时间设置为 30 秒。当我加载一个很大的页面时,我遇到了这个问题,该页面来自数据库的大量数据。其他因素也会导致此问题,例如大图像、大量 javascript 文件和大图标。我通过在 laravel 应用程序的入口点添加这一行来解决这个问题 public\index.php

 ini_set('max_execution_time', '60');

我为我的整个 laravel 应用程序做了这个。但是在您的情况下,您可以在导致此问题的某些页面上执行此操作。只需在特定页面的 php 脚本开头写下该行

原文由 Danish Mehmood 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题