php 如何解决 Laravel 5 中的超时错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30270316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve a timeout error in Laravel 5
提问by Geordie Gadgie
I have the following set up:
我有以下设置:
In routes I have:
在路线中,我有:
Route::get('articles', 'ArticlesController@index');
Route::get('articles', 'ArticlesController@index');
The index method in the controller is simply:
控制器中的 index 方法很简单:
public function index()
{
$articles = Article::all();
return View('articles.index', compact('articles'));
}
and in the view:
并在视图中:
@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
I attempted to replace the:
我试图替换:
$articles = Article::all();
with
和
$article = Article::latest()->get();
such that I can actually show the articles latest first. I got the error:
这样我就可以首先显示最新的文章。我得到了错误:
FatalErrorException in Str.php line 322:
Maximum execution time of 30 seconds exceeded
and the call stack is:
调用堆栈是:
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
... 等等
I have restored the controller method to what it was, but the error persists.
我已将控制器方法恢复到原来的状态,但错误仍然存在。
Can you please tell me how I can solve this problem?
你能告诉我如何解决这个问题吗?
回答by Noman Ur Rehman
The Maximum execution time of 30 seconds exceedederror is not related to Laravel but rather your PHP configuration.
超过 30 秒的最大执行时间错误与 Laravel 无关,而是与您的 PHP 配置有关。
Here is how you can fix it. The setting you will need to change is max_execution_time
.
这是您可以修复它的方法。您需要更改的设置是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)
You can change the max_execution_time
to 300
seconds like max_execution_time = 300
您可以max_execution_time
将300
秒更改为max_execution_time = 300
You can find the path of your PHP configuration file in the output of the phpinfo
function in the Loaded Configuration File
section.
您可以phpinfo
在Loaded Configuration File
部分的函数输出中找到 PHP 配置文件的路径。
回答by Grigoreas P.
it's a pure PHP setting. The alternative is to increase the execution time limit only for specific php scripts, by inserting on top of that php file, the following:
这是一个纯 PHP 设置。另一种方法是仅针对特定的 php 脚本增加执行时间限制,方法是在该 php 文件的顶部插入以下内容:
ini_set('max_execution_time', 180); //3 minutes
回答by Umar Tariq
In Laravel:
在 Laravel 中:
Add set_time_limit(0) line on top of query.
在查询顶部添加 set_time_limit(0) 行。
set_time_limit(0);
$users = App\User::all();
It helps you in different large queries but you should need to improve query optimise.
它可以帮助您处理不同的大型查询,但您应该需要改进查询优化。
回答by Alireza Aboutalebi
Sometimes you just need to optimize your code or query, Setting more max_execution_time is not a solution.
有时你只需要优化你的代码或查询,设置更多的 max_execution_time 不是一个解决方案。
A code should not be run more than 30s.
代码运行时间不应超过 30 秒。
回答by karrar kazuya
try
尝试
ini_set('max_execution_time', $time);
$articles = Article::all();
where $time
is in seconds, set it to 0 for no time. make sure to make it 60 back after your function finish
其中$time
以秒为单位,立即将其设置为 0。确保在功能完成后将其设为 60
回答by Mahendra Pratap
set time limit in __construct method or you can set in your index controller also where you want to have large time limit.
在 __construct 方法中设置时间限制,或者您也可以在索引控制器中设置时间限制。
public function __construct()
{
set_time_limit(8000000);
}
回答by sanjay
Add above query
添加上面的查询
ini_set("memory_limit", "10056M");
ini_set("memory_limit", "10056M");
回答by DonKoko
If you are hosting your Laravel app on Heroku, you can create custom_php.ini
in the root of your project and simply add max_execution_time = 60
如果你在 Heroku 上托管你的 Laravel 应用程序,你可以custom_php.ini
在你的项目的根目录中创建并简单地添加max_execution_time = 60
回答by shriyash deshmukh
Execution is not related to laravel go to the php.ini file In php.ini file set max_execution_time=360 (time may be variable depends on need) if you want to increase execution of a specific page then write ini_set('max_execution_time',360) at top of page
执行与laravel无关,请转到php.ini文件在php.ini文件中设置max_execution_time=360(时间可能因需要而异)如果你想增加特定页面的执行然后写ini_set('max_execution_time',360 ) 在页面顶部
otherwise in htaccess php_value max_execution_time 360
否则在 htaccess php_value max_execution_time 360
回答by Kwaye Kant
If using PHP7, I would suggest you changing the default value in the public/.htaccess
如果使用 PHP7,我建议您更改 public/.htaccess 中的默认值
<IfModule php7_module>
...
php_value max_execution_time 300
...
</IfModule>