将公共目录更改为 public_html 后的 Laravel

Posted

技术标签:

【中文标题】将公共目录更改为 public_html 后的 Laravel【英文标题】:Laravel after changing public directory to public_html 【发布时间】: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');怎么样? 这能回答你的问题吗? How to point laravel project to a public folder under public_html in shared hosting 【参考方案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 与上述解决方案一起使用。

【讨论】:

以上是关于将公共目录更改为 public_html 后的 Laravel的主要内容,如果未能解决你的问题,请参考以下文章

如何将文档根目录更改为脚本的公共目录?

使用 terraform 将公共 GKE 更改为私有 GKE 集群

在派生类中使用指令将继承更改为公共

Swig:如何将生成的 C# 类的可访问性从“公共类”更改为“内部类”

R8 在没有 -allowaccessmodification 标志的情况下将抽象类的“受保护”方法更改为“公共”

将 NSNumber 更改为 Int/Double 后的错误