2

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?

Sayakiss
  • 6,878
  • 8
  • 61
  • 107

1 Answers1

2

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.

Narf
  • 14,600
  • 3
  • 37
  • 66
  • You're right that PCNTL maybe disabled in the environment. However, as OP has it the function is resolved from the wrong namespace. If it exists in OP's environment, OP has to reference it from the global namespace e.g. `\pcntl_fork();` – Oluwafemi Sule May 14 '19 at 05:52
  • 1
    Not just maybe; it's not a matter of misconfiguration ... by disabled I mean it won't register itself even if you try to load it. As for the namespace thing, the error message will show it in the current namespace as that is what it first tries to resolve, but behind the scenes it does try to find it in the root namespace as well; it doesn't need to be explicitly declared as being there (although that wouldn't hurt). – Narf May 14 '19 at 06:16