如何在 Laravel 5 中设置基本路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31805814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:07:02  来源:igfitidea点击:

How to set the base path in Laravel 5

phplaravel

提问by Abel

I moved Laravel's public folderto the root folder, and I moved Laravelto its own folder. So I can use Laravel on a shared hosting. It looks like this:

我将Laravel 的公共文件夹移动到根文件夹,并将Laravel移动到它自己的文件夹。所以我可以在共享主机上使用 Laravel。它看起来像这样:

2015/08/04  18:13    <DIR>          .
2015/08/04  18:13    <DIR>          ..
2015/08/01  17:50               896 .htaccess
2015/07/29  17:39                 0 favicon.ico
2015/07/29  17:39             1,844 index.php
2015/08/04  17:19    <DIR>          laravel
2015/08/04  17:20    <DIR>          public
2015/08/01  17:46             1,165 README.md
2015/08/01  16:18                34 robots.txt
2015/08/04  17:20    <DIR>          static

For example, I'm using public_path()and I get this:

例如,我正在使用public_path(),我得到这个:

/htdocs/laravel/public

/htdocs/laravel/公共

but what I need is this:

但我需要的是:

/htdocs/public

/htdocs/公共

I've checked config files in /config/but there is nothing about it.

我已经检查了配置文件,/config/但没有任何相关信息。

Please tell me how to fix it. thx

请告诉我如何修复它。谢谢

(My laravel version is 5.0.33)

(我的 Laravel 版本是5.0.33



PS: I've tried this method: Laravel 5 on shared hosting - wrong public_path()

PS:我试过这种方法: Laravel 5 on shared hosts - wrong public_path()

I overwrited public_path()in AppServiceProvider

我覆盖public_path()AppServiceProvider

$this->app->bind('path.public', function() {
    return base_path() . '/';
});

But nothing changed.

但什么都没有改变。

回答by Emeke Ajeh

you can bind the public folder correctly by adding this to the index.php file in usually found in your /public folder

您可以通过将此添加到通常在您的 /public 文件夹中找到的 index.php 文件来正确绑定公共文件夹

// set the public path to this directory
$app->bind('path.public', function() {
    return __DIR__;
});

You may want to clear your cache after this as the paths may have been cached before. The cached config can be located and deleted manually if you don't have ssh access to the server at bootstrap/cache/config.php

您可能希望在此之后清除缓存,因为之前可能已缓存路径。如果您没有 ssh 访问服务器,则可以手动定位和删除缓存的配置bootstrap/cache/config.php

回答by Mush

Instead of moving the public folder, try to add a .htaccess in your root with the following content :

不要移动公共文件夹,而是尝试在您的根目录中添加一个 .htaccess 并包含以下内容:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ public/    [L]
   RewriteRule    (.*) public/ [L]
</IfModule>

This will redirect all the requests to /public

这会将所有请求重定向到 /public