【问题标题】:Laravel after changing public directory to public_html将公共目录更改为 public_html 后的 Laravel
【发布时间】:2021-08-10 09:09:43
【问题描述】:

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

  • Laravel 版本:5.7
  • Dompdf:“barryvdh/laravel-dompdf”:“^0.9.0”

我收到此错误:

错误异常: file_put_contents(/var/www/html/myproject/public_html/user_invoices/invoice_order_447B11621531373227.pdf): 无法打开流:中没有这样的文件或目录 /var/www/html/crm/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:122

这是我的可邮寄课程:

<?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';
        });
  }
}

【问题讨论】:

  • 你试过了吗? $this->app->bind('path.public', function() { return base_path('public_html'); });
  • 由于服务器的文件夹结构,您不应该更改公共路径。进入public_html/index.php,根据应用路径更新所需文件的路径。其他一切都会正常工作。
  • 将您的应用程序文件从/var/www/html/myproject 移动到/var/www/html,然后检查以确保config/filesystems.php 引用了正确的公共路径
  • 更改config('filesystems.disks.public.root');怎么样?

标签: php laravel dompdf


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    已解决:

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

    Laravel showing "Failed to clear cache. Make sure you have the appropriate permissions"

    最后 php artisan cache:clear 与上述解决方案一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-19
      • 2016-12-23
      • 1970-01-01
      • 2012-09-26
      • 2015-07-23
      • 2021-07-23
      • 1970-01-01
      • 2016-07-25
      相关资源
      最近更新 更多