Laravel 5: Generating Paths Relative to the Application Root With app_path

April 14, 2018 —John Koster

The app_path will return the full path to the Laravel application directory (which is named app by default). The exact value returned by calling app_path is dependent on the specific folder structure where the application is residing.

#Signature

The signature of the app_path function is:

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

#Example Use

An example path returned might look like this:

1/home/vagrant/Code/Laravel/app

The app_path function can also be used to build paths relative to the application directory. This is accomplished by supplying a string as the first and only argument. The following example will make numerous calls to app_path. The resulting path will appear above the function call as a comment.

1// /home/vagrant/Code/Laravel/app/Commands
2app_path('Commands');
3 
4// /home/vagrant/Code/Laravel/app/Console
5app_path('Console');
6 
7// /home/vagrant/Code/Laravel/app/Console/
8app_path('Console/');

It should be noted that the app_path function does not automatically add the trailing forward slash ('/') character when constructing the final string. This can be observed in the last two function calls in the example. If it is important that the path returned must always have a trailing forward slash, the return value of the app_path can be passed into the str_finish function like so:

1// /home/vagrant/Code/Laravel/app/Console/
2str_finish(app_path('Console'), '/');
3 
4// /home/vagrant/Code/Laravel/app/Console/
5str_finish(app_path('Console/'), '/');

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.