Laravel 将 public 目录更改为 public_html 后

Laravel after changing public directory to public_html

我在 public 目录为 public_html 的共享服务器上托管了我的 laravel 应用程序。订单发票路径应为 /var/www/html/public_html/user_invoices/invoice_order_447B11621531373227.pdf

我收到这个错误:

ErrorException: file_put_contents(/var/www/html/myproject/public_html/user_invoices/invoice_order_447B11621531373227.pdf): failed to open stream: No such file or directory in /var/www/html/crm/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:122

这是我的可邮寄 class:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use PDF;

class UserNotificationsOrderPlaced extends Mailable
{
    public $data;
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {           

        $path = public_path('user_invoices/invoice_order_'.$this->data[0]->order_id.'.pdf');            
        $pdf = PDF::loadView('pdf.invoice', ['order'=>$this->data[0]])->save($path);        
        return $this->view('emails.orders')->with(['orders'=>$this->data])->attach($path);

            
    }
}

AppServiceProvider:

我已经尝试了 realpath 和 base_path 两种解决方案,但仍然出现相同的错误:

return realpath(base_path().'/../public_html');
return base_path().'/../public_html';

return realpath(base_path().'/public_html');
return base_path().'/public_html';

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //Schema::defaultStringLength(191);
        if (class_exists('Swift_Preferences')) {
            \Swift_Preferences::getInstance()->setTempDir(storage_path().'/tmp');
        } else {
            \Log::warning('Class Swift_Preferences does not exists');
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
  public function register()
  {
        $this->app->bind('path.public', function() {
            return realpath(base_path().'/../public_html');    
            //return base_path().'/../public_html';
        });
  }
}

您需要创建一个路由来清除服务器上的缓存

Route::get('/clear-cache', function() {
    $exitCode = Artisan::call('cache:clear');
    $exitCode = Artisan::call('config:cache');
    return 'DONE'; //Return anything
});

已解决:

我已经手动删除了缓存中的所有内容,但没有用。 这有助于我解决问题。

最终 php artisan cache:clear 使用上述解决方案。