Lesson 7 – Laravel Directory Structure

Sometimes, it becomes important to change the name of your directory to something else. It seems a difficult task but it is not. Laravel 5 makes it easier to change the name of any directory you want. In our example, we are going to change the name of the app directory to src.

We have to override the application class that has the directory name app hardcoded into it. First of all, create a new file named Application.php and put it in the app directory. It is the same directory whose name you are going to change. The following code shows how the overridden directories should look like. You can simply change the string “src” if you want to give your directory a different name.

namespace App;
class Application extends \Illuminate\Foundation\Application
 * @inheritdoc
 public function path($path = '')
 return $this->basePath . DIRECTORY_SEPARATOR . 'src' . ($path ? DIRECTORY_SEPARATOR .
$path : $path);

Save the file as our work here is finished.

Lesson 1 – Laravel Installation.

Once you have saved your file, you also need to call the new class. Open the boostrap/app.php directory and go to the following.

$app = new Illuminate\Foundation\Application(

Now, replace the above code with the following.

$app = new App\Application(

It is also necessary to change autoloading in the composer.jason file and match it with your new location. This is how you can do this.

"psr-4": {
    "App\\": "src/"

The final step is to run the composer dump-autoload command in the command prompt. As a result, the src directory will start serving your app.

It is also very convenient to change the name of the Controllers directory. All you have to do is to rename the default “controllers” directory or move it to where you want it. For example, you can move it from app/http/controllers and put it directly in the app folder like app/controllers.

Lesson 8 – Error Handling in Laravel

The “controllers” folder also contains many files itself. It is important to update the namespaces of all these files as well. Also make sure they follow the new path which in turn should respect the PSR-4 specific.

Now, open app\Providers\RouteServiceProvider.php and change:  

protected $namespace = 'App\Http\Controllers';

With the following.

protected $namespace = 'App\Controllers';

In the next lesson, we will discuss Error Handling in Laravel.