Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NathanIsaac's avatar

Run PHP Artisan Optimize from Controller

Hello,

Is there a way to run php artisan optimize --force from a Laravel 4.2 controller?

I tried to call Artisan::call('optimize', ['--force' => true]) but I get an this error Call to undefined method Illuminate\Support\Facades\Artisan::call().

Any thoughts on how to get around this would be appreciated. Thanks.

0 likes
7 replies
michaeldyrynda's avatar

How are you using Artisan::call in your controller?

Did you import the Facade (use Artisan) or the class (use Illuminate\Support\Facades\Artisan)? The former should work, as should calling \Artisan::call(...).

rohit's avatar

@nisaac2fly Try this..

use Artisan;

In the method you are trying to use the artisan command do this

try {
    Artisan::call('optimize', ['--quiet' => true, '--force' => true]);
} catch(Exception $e) {
    print_r($e);
}

This should atleast tell you where the error is if any. If this doesn't work, please post entire code of what you are trying..

willvincent's avatar

You could probably bypass artisan and call it directly.

You probably won't be able to pass the force parameter to it, but you could just call the methods that executes. So this may get the job done for you:

$optimize = new \Illuminate\Foundation\Console\OptimizeCommand();
$optimize->compileClasses();
$optimize->compileViews();
NathanIsaac's avatar

@willvincent @rohit @deringer

After some more debuging this morning. I found that after installing a fresh install of Laravel 4.2 running Artisan::call('optimize', ['--quiet' => true, '--force' => true]) worked great.

Now it is a mater of trying to figure out what is different from my app vs a fresh install.

This is what my controller looks like:

<?php namespace Controllers\Manage;

use Illuminate\Console\Application;

class ManageSiteController extends \BaseController {

    private $artisan;

    function __construct(Application $artisan)
    {
        $this->artisan = $artisan;
    }
    
    public function index()
    {
        return \View::make('manage.site.index');
    }

    public function store()
    {
        try
        {
            $this->artisan->call('optimize', ['--quiet' => true, '--force' => true]);
        }
        catch(\Exception $e)
        {
            \Flash::error('Error occurred with optimization', $e->getMessage());

            return \Redirect::back();
        }

        \Flash::success('Optimization Successful');

        return \Redirect::back();
    }
}

After running this I get this error: ReflectionException (-1) Class artisan does not exist.

If I replace $this->artisan->call('optimize', ['--quiet' => true, '--force' => true]); with \Artisan::call('optimize', ['--quiet' => true, '--force' => true]); I get this error: Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR) Call to undefined method Illuminate\Support\Facades\Artisan::call().

@willvincent I tried your method but I get a error because those methods are protected.

willvincent's avatar

@nisaac2fly my suggestion was also based on Laravel 5, didn't realize you were using 4.2. But you're right, it is a protected method.. I didn't look that closely last night. Must've been tired :)

NathanIsaac's avatar
NathanIsaac
OP
Best Answer
Level 13

So I figured out the problem. My site was being accessed over a UNC path, which ended up being the problem. Moving the site to a local drive fixed the issue.

Please or to participate in this conversation.