Laravel, dump-autoload without Shell Access

2019-06-19 11:41发布

问题:

I have two controllers with the same name:

app\controllers\CareersController.php (for public use) app\controllers\Admin\CareersController.php (for admins)

Because of the naming conflict, I added namespace admin; to the admin controller.

Everything works fine locally but when I uploaded the new admin controller to my server, I get an error: Class Admin\CareersController does not exist

From what I understand, the fix is: php artisan dump-autoload and composer dump-autoload

However, I don't have Shell access to run those commands and composer isn't installed on the server anyway. So, is there a way to reload the auto-load file without Shell access?

回答1:

You dont need shell access. Artisan includes a dump-autoload function. You can just it via a PHP call within your app:

Route::get('/updateapp', function()
{
    \Artisan::call('dump-autoload');
    echo 'dump-autoload complete';
});

Edit: just noticed you wrote "composer isn't installed on the server anyway". Not sure what will happen - try the command above and let us know.

If it doesnt work - then just run composer dump-autoload locally - then upload your new autoload.php.

As a side point - is there any option to switch servers? You going to keep running into various issues if you dont have command line & composer access. You could just use Forge and spin up a new server on DigitalOcean, Linode etc in less time than it would take to fix this issue :)



回答2:

I was using a shared hosting by client's requirements and did not have access to ssh or composer, what I did was to composer dump-autoload on my local machine and then I figured that for my project autoloader just updates composer directory in my vendor directory, so I just re-uploaded that one folder after each dump-autoload and not all vendor directory

Edit:

Another pitfall for me that generated the same error but the cause was something else, I develop on Windows machine in which file and directory names are case insensitive when deploying to Linux server, the framework could actually not find my controllers so I changed

Route::get('/news', 'newsController@index');

to

Route::get('/news', 'NewsController@index');

now it is working, autoload is doing it's job correctly