phplaravelpcntl

Why pcntl_fork() not defined in Laravel Controller?


I have Controller:

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(){}

    public function index()
    {
        pcntl_fork();
    }
}

Then I call index() by HTTP request, And I get:

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to undefined function App\Http\Controllers\pcntl_fork()

Then I try this:

class CodeSheet extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'code_sheet';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        pcntl_fork();
        echo "1\n";
    }
}

Then I call this command:

vagrant@homestead:~$ php artisan code_sheet
1
1

So my question is, Why I can call pcntl_fork() in command, but can't in HTTP request?


Solution

  • The PCNTL extension is disabled in web environments (it probably even only compiles for the CLI SAPI), this has nothing to do with Laravel.

    The reason for that is simple - the web server itself (or PHP-FPM) is in control of process management, so using this extension would create a conflict with it.

    It's also not available under Windows as it's a UNIX-specific thing.