【问题标题】:laravel artisan served Server hungs while requesting a route on api.php within same project?laravel artisan 在同一项目中请求 api.php 上的路由时服务服务器挂起?
【发布时间】:2019-06-04 20:48:54
【问题描述】:

我在Routes 文件夹中的api.php 如下所示:

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('login', 'Api\User\LoginController@login');

web.php如下:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/system/login', 'System\LoginController@index');

这是System\LoginController.phpindex 方法上的代码:

class LoginController extends Controller
{
    use Requestable;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $response = $this->post('api/login',$request->all());
        $responseArray = json_decode($response->getBody(), true);
        session()->put('access_token',$responseArray['data']['token']);
        return redirect()->to('/system/dashboard');

        //
    }

所以基本上,我在同一个项目中使用 api。项目文件夹在 xampphtdocs 内。所以每当我打开并使用 Apache 服务器时,api 发送数据就好了。但是,如果我使用 @ 987654332@为项目服务的命令,每次我尝试从api获取数据时,服务器都不会抛出任何错误,而是挂断,即使经过很长时间也不返回数据。我想这是服务器如何处理工匠服务的问题。请有人帮我解决这个问题?

【问题讨论】:

  • htdocs of xampp,请使用 xampp 内置 Web 服务器 (apache) 而不是使用 php artisan serve。问题可能在于 serve 命令仅生成一个 php 进程实例来处理请求这一事实。因此,它卡住了,因为它在等待自己。请参阅this discussion 了解更多信息。

标签: php laravel laravel-5 laravel-artisan


【解决方案1】:

如果你仔细观察,这就是它的工作原理。

当你在浏览器中点击/system/login(Web 请求)时,它会触发 php,它会寻找正确的路由,恰好是System\LoginController@index。 然后在 LoginController 中运行 index 函数。

现在它在运行中找到以下行(API 请求)

$response = $this->post('api/login',$request->all());

所以在这一行,同一个 php 实例,(正在为网络请求提供服务)尝试调用自己!并且网络请求还没有完成。另外请记住 PHP 的同步特性,直到当前行返回之前它不会移动到下一行。

如果你注意到here PHP 的内置服务器是一个单线程进程

Web 服务器只运行一个单线程进程,因此如果请求被阻止,PHP 应用程序将停止。

它一次只能处理一个请求。因此,API 请求只是等待(在队列中)被处理,因为 Web 请求尚未完成,而 Web 请求正在等待其刚刚进行的 API 调用的响应.这种死锁是一切都被挂起的原因。在像 Apache 服务器这样的生产服务器的情况下,这不会发生,因为 Apache 可以在需要时产生多个进程/线程并将它们委托给单独的 php 实例进行处理.

【讨论】:

    猜你喜欢
    • 2017-05-24
    • 2020-08-31
    • 2016-04-16
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2019-04-05
    • 1970-01-01
    相关资源
    最近更新 更多