How Change laravel public_path - laravel

I changed my laravel installation folder structure by moving all files and folders from the root directory to a new folder 'core'.
The goal is, to have only the folders in the root directory of the project
core
public
I have updated the index.php in public folder but I have the following problem:
when I die dump the paths, all are correct except the public path
dd(app_path()); // somepath\project\core\app
dd(storage_path()); // somepath\project\core\storage
dd(base_path()); // somepath\project\core
dd(public_path()); // somepath\project\core\public
I want to retain all other paths and change only the public path to somepath\project\public instead of somepath\project\core\public , how can I do this, online docs I've reference focus on changing public_html to public but I don't want to change the public_html, just to tell laravel that the public path is somepath\project\public
This particularly needful for me as I use the laravel storage for handling uploads.
Below is my folder structure
project
|-core
|- every other folders and files
|-public
|-every other asset files

You need to rewrite the base path in the app/Providers/AppServiceProvider.php like so:
public function boot()
{
$this->app->bind('path.public', function () {
return dirname(getcwd(), 1) . '/public';
});
}

Related

Using public and resources folder as alias in laravel

I split a laravel project into 2 parts, backend and frontend, I created 2 repos on GitHub.
In the frontend repo, there are only 2 folders, public and resources folder.
So, when I pull the changes on frontend, I would like all files to replace in backend folder too.
------ backend folder
---- public folder -> I want to it be an alias
---- resources folder -> I want to it be an alias
----- frontend folder
---- public folder
---- resources folder
When I create an alias of the public and resources folder in frontend, laravel won't work.
If I create a symlink of those frontend folders, laravel works but index.php file doesn't work as it doesn't follow symlinked path
How can I get it working?
To change public folder directory go to root index.php
Under the $app = require_once __DIR__.'/../bootstrap/app.php'; add below line:
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
To change the resource of view folder directory go to config/view.php and change your desire path.
'paths' => [
realpath(base_path('resources/views'))
],

How to create thumbnail image in laravel using Intervention in live server?

My primary domain is xyz.com. I have created a folder named "test" outside public_html folder and uploaded the whole laravel set up there except the public folder which I have moved to public_html folder.
My folder structure is like this.
test -> contains the laravel project without public folder.
public_html -> contains the public folder.
However I cannot upload images. Error is showing like "Image source not readable". Please help.
if ($request->hasFile('frontimage')) {
$file_frontimage = $request->file('frontimage');
$actual_filename_frontimage = $file_frontimage->getClientOriginalName();
$filename_frontimage = time() . '_' . $actual_filename_frontimage;
$file_frontimage->storeAs('images', $filename_frontimage, 'public');
}

Change public directory laravel

I want to change the public folder to public_html, the main problem is that some commands and functions not work because it still point to public.
I am trying to use the laravel+vue to develop a spa. i normally rename the public folder to public_html and that's all, but know it seems that i have to use the public_path helper and the console and these use the public folder too.
im using npm run dev as in the laravel docs, but it generate everything in the public folder, not in the public_html that i have renamed
so how can i tell the console and the public_path helper to do they magic in the public_html folder?
I am using Laravel 5.6, it is a fresh project, with just a controller and a view with a mix helper calling the js that i want link this <script src="{{ mix('js/app.js') }}"></script>.
May this can help you. You can override Laravel default helper methods. This code helped me at a time when I got same problem like you.
Simple I created one helper file and override public_html function something like this:
/**
* Get the path to the public folder.
*
* #param string $path
* #return string
*/
function public_path($path = '')
{
return base_path().'/public_html';
}
Let's say file name is AppHelper.php and located into app/Http folder.
To override helper from base you need to include your helper file(AppHelper.php) before Laravel does it's own autoload file.
So for the web you have to include your file into index.php like this:
require __DIR__.'/../app/Http/AppHelper.php';
require __DIR__.'/../vendor/autoload.php';
Remember your helper must be included before autoload file. and this code snippet for Command line or Artisan CLI:
require __DIR__.'/app/Http/AppHelper.php';
require __DIR__.'/vendor/autoload.php';
You have to write your artisan cli changes into artisan file which is on root.
In Laravel if you check helper function then each and every function wrapped with if condition like this function is not exist then declare it using function_exists()
In our case we already declare public_path function and included before Laravel autoload file so every request first execute our function.
Hope this can help you. Good luck.
Add this in register() method of your AppServiceProvider
public function register()
{
// Add this part
$this->app->bind('path.public', function() {
return base_path().'/public_html';
});
}
For the css+js resources, as #Bagus Tesa said, you have a file named webpack.mix.js in the root of your application folder. It's not you who created it, it comes with the laravel project. In that file you'll find something like:
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');
Change the destination path from public to public_html

Uploading images to Public_html file rather than public project file

Had this working but just updated my website on the server and it seems to have made all my uploaded files head to my public folder in my project rather than the public_html. Any idea on which file i must have uploaded and the fix?
You said you had it working before.
Was that on the same server...live?
If so, then could that be a server issue and not necessarily laravel issue?
Maybe you should ask your host for help and clarifications about what changed?
If you are convinced it was about what you changed...then care to show us?
Ideally if the server is cpanel, you will want to upload all your laravel files into your home's directly...then all your /public folder content into public_html
It will look like this:
.cpanel/
app/
etc/
bootstrap/
mail/
public_html/
content from your laravel /public directory
vendor/
...etc
You also need to go into your /public_html and edit the index.php content:
require __DIR__.’/../vendor/autoload.php’;
$app = require_once __DIR__.’/../bootstrap/app.php’;
And make sure it points to the correct location of your bootstrap folder..if you follow the structure above, then its ok.
Lastly, confirm that your PHP is indeed >= PHP7.0
With help from this answer: https://stackoverflow.com/a/50261573/3475551
I managed to get laravel to save uploaded files to my servers public directory instead of app/public.
Edit app/Providers/AppServiceProvider.php like this:
public function register()
{
$this->app->bind('path.public', function() {
return realpath(base_path().'/../../public_html');
});
}
Hope this helps.

Url with public

I am using laravel and deployed my application on server
application folder path /var/www/app
So when i run the url with public, css will load.
http://xxx.xxx.xxx.xxx/app/public/
But when i remove the public from url, css will not load.
http://xxx.xxx.xxx.xxx/app/
Css and js files are located into the public folder
How can i solve this issue, can i change the asset path if yes please tell me the where the asset define
Thanks
Laravel is designed to run the code from the public directory, so files like .env are protected from public access. Your server should be pointed to the public folder.
However, if you want to change the public url, you can edit the index.php file and specify your own path:
// add after $app = require_once... in index.php
$app->bind('path.public', function() {
return __DIR__;
});

Resources