How to change public folder to public_html in laravel 5
Quite easy to find this with a simple search.
See: https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5
In your index.php add the following 3 lines.
/*
|--------------------------------------------------------------------------
| 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__;
});
Edit:
As Burak Erdem mentioned, another option (and more preferable) is to put this in the \App\Providers\AppServiceProvider
register()
method.
/**
* Register any application services.
*
* @return void
*/
public function register()
{
// ...
$this->app->bind('path.public', function() {
return base_path('public_html');
});
}
Server
Methods described in topic are working just fine, so modyfing App\Providers\AppServiceProvider.php register method should do the job:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
public function register()
{
$this->app->bind('path.public', function() {
return base_path() . '/public_http';
});
}
Local php artisan serve development
However, there is one more issue you can experience. If you're developing your app on local machine and you're using php artisan serve
command to serve your app you're going to break it with above syntax only. You still need to adjust server.php file which exists in main directory. Edit the contents of it and replace each occurance of /public
to /public_html
, so it looks like this:
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <[email protected]>
*/
$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';
After that. Just stop your server and reload it with php artisan serve
.
Front end laravel-mix development
If you're using webpack and laravel-mix to generate your css and js files then this also needs some update. Without tweaking webpack.mix.js you will end up with something like this on npm run watch
or npm run production
:
.
..
public
_html
js
public_html
css
public_html
So it's going to mess up your code. To clarify this you have to provide a public path to your webpack.mix.js file. It could look like this:
const mix = require('laravel-mix');
mix.setPublicPath('public_html/');
mix.js('resources/js/app.js', 'js')
mix.sass('resources/sass/app.scss', 'css');
This is going to change the default definition of public directory from public
to public_html
and next lines provides a relative path to your setPublicPath value.
Happy coding.
If Robert's index.php
solution is not working for you, you can also register the following code at Application Service Provider (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';
});
}