Laravel 5: Generating Paths Relative to the Public Directory With public_path

April 14, 2018 —John Koster

The public_path will return the path to the public directory. It can also be used to construct paths relative to the public directory if a $path is supplied.

#Signature

The signature of the public_path function is:

1function public_path(
2 $path = ''
3);

#Example Use

The following examples will assume that the Laravel application is installed in the
/home/vagrant/Code/Laravel/ directory. The resulting path will appear above the function calls as a comment.

1// /home/vagrant/Code/Laravel/public
2public_path();
3 
4// /home/vagrant/Code/Laravel/public/js
5public_path('js');

The public_path does not automatically add the trailing slash / character to the final string. public_path's return value can be passed into the str_finish function to achieve the same affect:

1// /home/vagrant/Code/Laravel/public/js/
2str_finish(public_path('js'), '/');

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.