我如何更改Laravel中的公共/目录名称9

发布于 2025-02-12 22:42:16 字数 1598 浏览 0 评论 0原文

从3个月前开始,我已经看到一个标题非常相似的问题,但没有答案。

经过新的Laravel 9安装后,将Laravel应用程序放入其自己的目录中的最低步骤是什么,还可以修改公共目录名称?拥有Laravel 9特定的逐步指南将很方便,因为我似乎在任何地方都找不到一个。

例如。以下根目录结构:

laravel-app/(包含所有Laravel文件,例如Resources/and Storage/)

public_html/(包含index.php和/js etc)

使用旧过程(类似于此 https://github.com/hannanstd/change-laravel-public )我一直使用不再起作用,这似乎与vite有关vite与laravel 9使用而不是laravel-mix有关。它可以工作到运行PHP Artisan Serve的点,但是在使用Vite运行NPM运行时失败。给出此错误:

$ npm run dev

> dev
> vite

(!) Could not auto-determine entry point from rollupOptions or html files and there are no explicit optimizeDeps.include patterns. Skipping dependency 
pre-bundling.
node:internal/fs/utils:347
    throw err;
    ^

Error: ENOENT: no such file or directory, open 'public\hot'
    at Object.openSync (node:fs:594:3)
    at Object.writeFileSync (node:fs:2207:35)
    at Server.<anonymous> (D:\Websites\laravel9-admin\laravel-core\node_modules\laravel-vite-plugin\dist\index.js:122:34)
    at Object.onceWrapper (node:events:627:28)
    at Server.emit (node:events:525:35)
    at emitListeningNT (node:net:1466:10)
    at processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -4058,
  syscall: 'open',
  code: 'ENOENT',
  path: 'public\\hot'
}

因此,似乎在其他地方定义了默认的“ public/”,但是在我的项目中使用Ctrl+Shift+F搜索后,它似乎并不重要,因此真的不确定它从哪里获得。任何帮助都非常感谢,但实际上是关于如何实现Laravel 9的逐步指南,这是最终目标,因此它可以成为我和其他有相同问题的用户的参考:)

I have seen a question with a very similar title on SO from 3 months ago but it has no answers.

After a fresh laravel 9 installation what are the minimum steps to put the laravel application into it's own directory, and also modify the public directory name? It would be handy to have a step by step guide with laravel 9 specific one as I can't seem to find one anywhere.

eg. the following root directory structure:

laravel-app/ (contains all laravel files like resources/ and storage/)

public_html/ (contains index.php and /js etc)

Using the old process (similar to this https://github.com/hannanstd/change-laravel-public) I have always used no longer works, and this seems to have something to do with vite that laravel 9 uses instead of laravel-mix. It works up to the point of running php artisan serve but fails when running npm run dev with vite. Giving this error:

$ npm run dev

> dev
> vite

(!) Could not auto-determine entry point from rollupOptions or html files and there are no explicit optimizeDeps.include patterns. Skipping dependency 
pre-bundling.
node:internal/fs/utils:347
    throw err;
    ^

Error: ENOENT: no such file or directory, open 'public\hot'
    at Object.openSync (node:fs:594:3)
    at Object.writeFileSync (node:fs:2207:35)
    at Server.<anonymous> (D:\Websites\laravel9-admin\laravel-core\node_modules\laravel-vite-plugin\dist\index.js:122:34)
    at Object.onceWrapper (node:events:627:28)
    at Server.emit (node:events:525:35)
    at emitListeningNT (node:net:1466:10)
    at processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -4058,
  syscall: 'open',
  code: 'ENOENT',
  path: 'public\\hot'
}

So it seems that yet somewhere else the default "public/" is defined, but after searching with ctrl+shift+F in my project it doesn't seem to be anywhere that matters so really unsure where it's getting it from. Any help is highly appreciated, but really a step by step guide on how to achieve this for Laravel 9 is the end goal so it can be a reference for myself and other users with the same problem :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北渚 2025-02-19 22:42:16

我自己回答这个解决方案!

第一个选项:仅重命名公共/目录,对于Laravel 9:

  1. 将公共文件夹重命名为public_html(或任何所需的名称
  2. ) AppServiceProvider.php ,并在注册方法中添加以下代码:
  $ this-&gt; app-&gt; bind('path.public',function(){
  返回realpath(base_path()。'/public_html');
});
 
  1. 添加publicDirectory:'public_html' laravel-core/vite.config.js在插件/laravel对象中,例如。
 导出默认decteconfig({
    插件:[
        laravel({
            publicDirectory:“ public_html”,
            输入: [
                “资源/sass/app.scss”,
                “资源/js/app.js”,
            ],,
            刷新:是的,
        }),
    ],,
});
 

第二个选项:将Laravel Core文件分开为自己的目录,并重命名公共/目录,对于Laravel 9:

  1. 将公共文件夹重命名为public_html(或任何所需的名称)
  2. 创建一个laravel核目录,然后将所有文件和文件夹移动以外的所有文件和文件夹,public_html添加到Laravel核心文件夹中。
  3. 编辑index.phppublic_html文件夹中,查找/替换__ dir __。 laravel-core/(3个情况)
  4. 编辑appserviceprovider.php,并在注册方法中添加以下代码:
  $ this-&gt; app-&gt; bind('path.public',function(){
  返回realpath(base_path()。'/../public_html');
});
 
  1. 添加publicDirectory:'public_html' laravel-core/vite.config.js在插件/laravel对象中,例如。
 导出默认decteconfig({
    插件:[
        laravel({
            publicDirectory:'../public_html',
            输入: [
                “资源/sass/app.scss”,
                “资源/js/app.js”,
            ],,
            刷新:是的,
        }),
    ],,
});
 

Answering this myself as found the solution!

First Option: Just rename the public/ directory, for laravel 9:

  1. Rename public folder to public_html (or any desired name)
  2. Edit AppServiceProvider.php and add below code in register method:
$this->app->bind('path.public', function() {
  return realpath(base_path().'/public_html');
});
  1. Add publicDirectory: 'public_html' to laravel-core/vite.config.js within the plugins / laravel object, eg.
export default defineConfig({
    plugins: [
        laravel({
            publicDirectory: 'public_html',
            input: [
                'resources/sass/app.scss',
                'resources/js/app.js',
            ],
            refresh: true,
        }),
    ],
});

Second option: Seperate out the laravel core files into their own directory and also rename the public/ directory, for laravel 9:

  1. Rename public folder to public_html (or any desired name)
  2. Create a laravel-core directory and move all files and folders except public_html into the laravel-core folder.
  3. Edit index.php in public_html folder and find/replace __DIR__.'/../ with __DIR__.'/../laravel-core/ (3 cases)
  4. Edit AppServiceProvider.php and add below code in register method:
$this->app->bind('path.public', function() {
  return realpath(base_path().'/../public_html');
});
  1. Add publicDirectory: 'public_html' to laravel-core/vite.config.js within the plugins / laravel object, eg.
export default defineConfig({
    plugins: [
        laravel({
            publicDirectory: '../public_html',
            input: [
                'resources/sass/app.scss',
                'resources/js/app.js',
            ],
            refresh: true,
        }),
    ],
});
自此以后,行同陌路 2025-02-19 22:42:16

对我来说,上面的解决方案部分工作。
这就是我使用Vite插件为Laravel 10制作的方式,因此它可以在服务器上使用,但也可以通过工匠在本地提供服务。

重命名public public_html
中vite.config.js add publicDirectory:'public_html'

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'resources/css/app.css',
                'resources/js/app.js',
            ],
            publicDirectory: 'public_html',
            refresh: true,
        }),
    ],
});

in appserviceprovider

public function register()
{
    app()->usePublicPath(realpath(base_path().'/public_html'));
}

和某些原因出于某种原因php Artisan Storage:link 在此之后停止工作,这样解决,请转到/foo以使SIMLINK或在终端中运行。

Route::get('/foo', function () {
    shell_exec('ln -sfn /path/to/storage/app/public/ /path/to/public_html/storage');
});

For me the solutions above worked partially.
This is how I made it for Laravel 10 with the Vite plugin, so it works on the server but also to serve it locally through artisan.

Rename public to public_html
In vite.config.js add publicDirectory: 'public_html'

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'resources/css/app.css',
                'resources/js/app.js',
            ],
            publicDirectory: 'public_html',
            refresh: true,
        }),
    ],
});

In AppServiceProvider:

public function register()
{
    app()->usePublicPath(realpath(base_path().'/public_html'));
}

And for some reason php artisan storage:link stops working after this, solved it like this, go to /foo to make the simlink or run it in the terminal if you have access.

Route::get('/foo', function () {
    shell_exec('ln -sfn /path/to/storage/app/public/ /path/to/public_html/storage');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文