laravel artisan served Server hungs while requesting a route on api.php within same project?

Viewed 241

My api.php inside Routes folder looks like this:

<?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');

and web.php is as follows:

<?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');

This is code on index method of System\LoginController.php:

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

        //
    }

So basically,I am consuming api within same project.The project folder is inside htdocs of xampp.So whenever I turn on and use Apache server,the api sends data just fine.However, if I use php artisan serve command to serve the project, every time I try to get data from api, Server doesn't throw any error but just hungs up and doesn't return data at all even after long time. I guess its the issue with how server behaves with artisan serve.Some body please help me get off of this issue?

1 Answers

If you look closely this is how it works.

When you hit /system/login (Web request) in your browser it triggers php, which goes looking for the correct route which happens to be System\LoginController@index. Then it runs the index function in LoginController.

Now as it finds the below line in the run (API request)

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

So at this line the same php instance, (was serving the web request) tries to call itself! and the web request still isn't complete yet.Also keep in mind the synchronous nature of PHP it won't move to next line until the current line has returned.

If you note here the PHP's inbuilt server is a single single-threaded process

The web server runs only one single-threaded process, so PHP applications will stall if a request is blocked.

It can process requests only one at a time.So the API request simply waits (in queue) to be processed as the Web request is still not done yet and Web request is waiting for an answer for its just-made API call.This deadlock is the reason why everything gets hanged up.In case of production servers like Apache server this doesn't happens as Apache can spawn-off multiple processes/threads as and when needed and delegate them to a separate php instance to be processed.

Related