Search

Laravel Application Helper: app_path

November 20, 2016 —John Koster

app_path($path = '')

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. 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<?php
2 
3// /home/vagrant/Code/Laravel/app/Commands
4app_path('Commands');
5 
6// /home/vagrant/Code/Laravel/app/Console
7app_path('Console');
8 
9// /home/vagrant/Code/Laravel/app/Console/
10app_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<?php
2 
3// /home/vagrant/Code/Laravel/app/Console/
4str_finish(app_path('Console'), '/');
5 
6// /home/vagrant/Code/Laravel/app/Console/
7str_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.