public_html
是默认的根目录,因此我无法使我的Laravel应用程序正常工作。有没有办法让Laravel使用
public_html
而不是public文件夹?通过简单的搜索就能轻松找到这个。
在您的index.php文件中添加以下3行代码。
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../bootstrap/app.php';
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
编辑:
正如Burak Erdem所提到的,另一种选项(更可取的)是将此放置在\App\Providers\AppServiceProvider
的register()
方法中。
/**
* Register any application services.
*
* @return void
*/
public function register()
{
// ...
$this->app->bind('path.public', function() {
return base_path('public_html');
});
}
index.php
解决方案对你不起作用,你也可以将下面的代码注册到应用服务提供商(App\Providers\AppServiceProvider.php
)中。<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
public function register()
{
$this->app->bind('path.public', function() {
return base_path().'/public_html';
});
}
public_html
而不是 public_http
? - hannebaumsaway服务器
主题中描述的方法运行良好,因此修改 App\Providers\AppServiceProvider.php 的 register 方法应该可以完成工作:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
public function register()
{
$this->app->bind('path.public', function() {
return base_path() . '/public_http';
});
}
本地 PHP artisan serve 开发
然而,你可能会遇到另一个问题。如果你在本地机器上开发应用程序并使用 php artisan serve
命令来提供服务,那么你只使用以上语法是无法正常工作的。你还需要调整位于主目录中的 server.php 文件。编辑它的内容,并将每个出现的 /public
替换为 /public_html
,使其看起来像这样:
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <taylor@laravel.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) {
return false;
}
require_once __DIR__.'/public_html/index.php';
之后,停止您的服务器并使用php artisan serve
重新加载它。
前端Laravel-mix开发
如果您正在使用webpack和laravel-mix生成您的css和js文件,则这也需要一些更新。如果不调整webpack.mix.js,则在npm run watch
或npm run production
上会出现以下结果:
.
..
public
_html
js
public_html
css
public_html
那么它会破坏你的代码。为了澄清这一点,您需要为webpack.mix.js文件提供公共路径。它可能看起来像这样:
所以它会破坏你的代码。为了澄清这一点,您需要为webpack.mix.js文件提供公共路径。它可能看起来像这样:
const mix = require('laravel-mix');
mix.setPublicPath('public_html/');
mix.js('resources/js/app.js', 'js')
mix.sass('resources/sass/app.scss', 'css');
这将更改公共目录的默认定义,从public
更改为public_html
,接下来的行提供了一个相对路径到您的setPublicPath值。
祝编码愉快!
./resources/js/components/SetBgImg.vue?vue&type=script&lang=js&":
,应该是 ./../projectname/resources/js/components/SetBgImg.vue?vue&type=script&lang=js&":
如何解决这个问题? - sharkyenergynpm run
以使更改生效。 - Fer Toasted请访问此网址:
/app/Providers/AppServiceProvider.php
并将此代码追加到文件末尾:
public function register()
{ $this->app->bind('path.public', function() {
return realpath(base_path().'/../public_html');
});
}
如果你的public_html不在laravel文件夹内,那么你需要使用以下代码进行更新:
$this->app->bind('path.public', function() {
return realpath(base_path().'/../public_html');
});
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
$app->usePublicPath(__DIR__.'/../../public_html');
$app->usePublicPath(str_replace('bootstrap', '', __DIR__).'\mysite.com');
- undefined在简单的根目录中创建 .htaccess 文件并添加以下代码:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
我花了两天时间才弄明白,但最终我通过以下步骤将我的Laravel项目部署到了我的cPanel Web主机:
if ($uri !== '/' && file_exists(__DIR__ . '/public_html' .$uri)) {
return false;
}
require_once __DIR__ . '/public_html/index.php';
mix.js('resources/js/app.js', 'public_html/js')
.postCss('resources/css/app.css', 'public_html/css', [
//
]);
public_html
文件夹。
2.通过ssh
连接到您的服务器。
3.在我的特定情况下,整个应用程序位于~/laravel
文件夹中。
4.现在您必须在~/
文件夹中:
5.运行rm -r public_html
。此命令将删除public_html文件夹及其所有内容。
6.创建符号链接ln -s $(pwd)/laravel/public $(pwd)/public_html
。$(pwd)
将被替换为从服务器根目录开始的绝对路径。
7.现在您应该能够看到所需的结果。如果需要更改公共路径以使其对Artisan也可用,请在最后一个singleton
方法之后添加以下内容到bootstrap/app.php
:
$app->bind('path.public', function () {
return base_path("<your public directory name>");
});
[ErrorException] chdir(): 没有这样的文件或目录(errno 2)
。 - Faisal Ahsanpath.public
更新为path.public_html
。至少对我来说是这样的。 :) - Carsten