简体   繁体   English

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

[英]How to solve a timeout error in Laravel 5

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: controller中的索引方法很简单:

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 ... ETC

I have restored the controller method to what it was, but the error persists.我已将 controller 方法恢复到原来的样子,但错误仍然存在。

Can you please tell me how I can solve this problem?你能告诉我如何解决这个问题吗?

The Maximum execution time of 30 seconds exceeded error 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.您可以在Loaded Configuration File部分的phpinfo函数的输出中找到 PHP 配置文件的路径。

it's a pure PHP setting.这是一个纯 PHP 设置。 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 文件的顶部插入以下内容:

ini_set('max_execution_time', 180); //3 minutes

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.它可以帮助您处理不同的大型查询,但您应该需要改进查询优化。

Sometimes you just need to optimize your code or query, Setting more max_execution_time is not a solution.有时你只需要优化你的代码或查询,设置更多的 max_execution_time 不是一个解决方案。

It is not suggested to take more than 30 for loading a webpage if a task takes more time need to be a queue.如果一个任务需要更多时间需要排队,不建议加载一个网页超过 30。

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);
}

try尝试

ini_set('max_execution_time', $time);
$articles = Article::all();

where $time is in seconds, set it to 0 for no time. $time以秒为单位,立即将其设置为 0。 make sure to make it 60 back after your function finish确保在功能完成后将其设为 60

Add above query添加上面的查询

ini_set("memory_limit", "10056M"); ini_set("memory_limit", "10056M");

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>

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

如果你在 Heroku 上托管你的 Laravel 应用,你可以在你的项目的根目录中创建custom_php.ini并简单地添加max_execution_time = 60

I had a similar problem just now.我刚才遇到了类似的问题。 However, this had nothing to do with modifying the php.ini file.但是,这与修改php.ini文件无关。 It was from a for loop.它来自 for 循环。 If you are having nested for loops, make sure you are using the iterator properly.如果您有嵌套的for循环,请确保正确使用迭代器。 In my case, I was iterating the outer iterator from my inner iterator.就我而言,我从内部迭代器迭代外部迭代器。

By default the max execution time set is 30 seconds.默认情况下,最大执行时间设置为 30 秒。 I'm facing this issue when I load a page which is quite big with huge data coming from database.当我加载一个很大的页面时,我遇到了这个问题,该页面来自数据库的大量数据。 Other factors will cause this issue too like big images, lot of javascript files and big icons.其他因素也会导致此问题,例如大图像、大量 javascript 文件和大图标。 I fixed this issue by adding this line in the entrypoint of laravel application public\index.php我通过在 laravel 应用程序public\index.php的入口点添加此行来解决此问题

ini_set('max_execution_time', '60'); 

I did this for my entire laravel application.我为整个 laravel 应用程序执行此操作。 But in your case, you can do this on certain pages which are causing this problem.但是在您的情况下,您可以在导致此问题的某些页面上执行此操作。 Just do write that line on start of php script for particular page只需在特定页面的 php 脚本的开头写下该行

Options -MultiViews -Indexes选项 -MultiViews -Indexes

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

#cambiamos el valor para subir archivos
php_value memory_limit 400M
php_value post_max_size 400M
php_value upload_max_filesize 400M
php_value max_execution_time 300 #esta es la linea que necesitas agregar.

You need to just press CTRL + F5 .你只需要按CTRL + F5 It will work after that.在那之后它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM